diff options
author | Luke Kanies <luke@madstop.com> | 2008-07-09 19:07:47 -0700 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-07-29 00:47:02 -0500 |
commit | 1f15725263812be347c42a2ea3777fee7b2129c9 (patch) | |
tree | c0d284d50d67f11c82992372a36f069364e4c31c | |
parent | 0e7e16d19b12cf7748215d3dd3adf5190fb90d20 (diff) | |
download | puppet-1f15725263812be347c42a2ea3777fee7b2129c9.tar.gz puppet-1f15725263812be347c42a2ea3777fee7b2129c9.tar.xz puppet-1f15725263812be347c42a2ea3777fee7b2129c9.zip |
Using the FormatHandler in indirected classes automatically.
Signed-off-by: Luke Kanies <luke@madstop.com>
Changing the FormatHandler#render method to render_to
Signed-off-by: Luke Kanies <luke@madstop.com>
Adding the ability for the format handler to query supported formats.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/indirector.rb | 2 | ||||
-rw-r--r-- | lib/puppet/network/format_handler.rb | 10 | ||||
-rwxr-xr-x | spec/unit/indirector.rb | 5 | ||||
-rwxr-xr-x | spec/unit/network/format_handler.rb | 30 |
4 files changed, 41 insertions, 6 deletions
diff --git a/lib/puppet/indirector.rb b/lib/puppet/indirector.rb index 2402b9cbe..1beb68ec0 100644 --- a/lib/puppet/indirector.rb +++ b/lib/puppet/indirector.rb @@ -10,6 +10,7 @@ module Puppet::Indirector require 'puppet/indirector/indirection' require 'puppet/indirector/terminus' require 'puppet/indirector/envelope' + require 'puppet/network/format_handler' # Declare that the including class indirects its methods to # this terminus. The terminus name must be the name of a Puppet @@ -22,6 +23,7 @@ module Puppet::Indirector extend ClassMethods include InstanceMethods include Puppet::Indirector::Envelope + extend Puppet::Network::FormatHandler # instantiate the actual Terminus for that type and this name (:ldap, w/ args :node) # & hook the instantiated Terminus into this class (Node: @indirection = terminus) diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb index a69c29232..ab828ad12 100644 --- a/lib/puppet/network/format_handler.rb +++ b/lib/puppet/network/format_handler.rb @@ -18,10 +18,18 @@ module Puppet::Network::FormatHandler def support_format?(name) respond_to?("from_%s" % name) and instance_methods.include?("to_%s" % name) end + + def supported_formats + instance = instance_methods.collect { |m| m =~ /^to_(.+)$/ and $1 }.compact + klass = methods.collect { |m| m =~ /^from_(.+)$/ and $1 }.compact + + # Return the intersection of the two lists. + return instance & klass + end end module InstanceMethods - def render(format) + def render_to(format) raise ArgumentError, "Format %s not supported" % format unless support_format?(format) send("to_%s" % format) end diff --git a/spec/unit/indirector.rb b/spec/unit/indirector.rb index 1efa7b2e5..c77ab0f62 100755 --- a/spec/unit/indirector.rb +++ b/spec/unit/indirector.rb @@ -54,6 +54,11 @@ describe Puppet::Indirector, "when registering an indirection" do @indirection = @thingie.indirects :first, :some => :options end + it "should extend the class with the Format Handler" do + @indirection = @thingie.indirects :first + @thingie.metaclass.ancestors.should be_include(Puppet::Network::FormatHandler) + end + after do @indirection.delete if @indirection end diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb index ab863fc8e..a63c26b08 100755 --- a/spec/unit/network/format_handler.rb +++ b/spec/unit/network/format_handler.rb @@ -7,6 +7,14 @@ require 'puppet/network/format_handler' class FormatTester extend Puppet::Network::FormatHandler + # Not a supported format; missing the 'to' + def self.from_nothing + end + + # Not a supported format; missing the 'from' + def to_nowhere + end + def self.from_good end @@ -23,8 +31,12 @@ describe Puppet::Network::FormatHandler do FormatTester.should be_support_format(:good) end - it "should not consider the format supported unless it can convert from an instance to the format and from the format to an instance" do - FormatTester.should_not be_support_format(:nope) + it "should not consider the format supported unless it can convert the instance to the specified format" do + FormatTester.should_not be_support_format(:nothing) + end + + it "should not consider the format supported unless it can convert from the format to an instance" do + FormatTester.should_not be_support_format(:nowhere) end it "should be able to convert from a given format" do @@ -41,6 +53,14 @@ describe Puppet::Network::FormatHandler do FormatTester.convert_from(:good, "mydata") end + it "should be able to list supported formats" do + FormatTester.should respond_to(:supported_formats) + end + + it "should include all formats that include both the to_ and from_ methods in the list of supported formats" do + FormatTester.supported_formats.should == %w{good} + end + describe "when an instance" do it "should be able to test whether a format is supported" do FormatTester.new.should respond_to(:support_format?) @@ -55,19 +75,19 @@ describe Puppet::Network::FormatHandler do end it "should be able to convert to a given format" do - FormatTester.new.should respond_to(:render) + FormatTester.new.should respond_to(:render_to) end it "should fail if asked to convert to an unsupported format" do tester = FormatTester.new tester.expects(:support_format?).with(:nope).returns false - lambda { tester.render(:nope) }.should raise_error(ArgumentError) + lambda { tester.render_to(:nope) }.should raise_error(ArgumentError) end it "should call the format-specific converter when asked to convert to a given format" do tester = FormatTester.new tester.expects(:to_good) - tester.render(:good) + tester.render_to(:good) end end end |