summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-07-28 18:05:41 -0500
committerLuke Kanies <luke@madstop.com>2008-07-29 00:51:22 -0500
commit55e29444c15fe2f810b3d5332605f27ac5942fda (patch)
tree5cd663fa5a55920f59f04dd4fa58677078d982b1
parent4632cfd9e6ce0ff59dfa7562a02a1ae3f14488d4 (diff)
downloadpuppet-55e29444c15fe2f810b3d5332605f27ac5942fda.tar.gz
puppet-55e29444c15fe2f810b3d5332605f27ac5942fda.tar.xz
puppet-55e29444c15fe2f810b3d5332605f27ac5942fda.zip
Adding support for rendering and converting multiple instances.
It's a touch hackish in terms of design, but it should work, and it's the last major piece of plumbing necessary for REST support. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/network/format_handler.rb16
-rwxr-xr-xspec/unit/network/format_handler.rb45
2 files changed, 52 insertions, 9 deletions
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
index b569bd696..f001f8242 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -15,6 +15,22 @@ module Puppet::Network::FormatHandler
send("from_%s" % format, data)
end
+ def convert_from_multiple(format, data)
+ if respond_to?("from_multiple_%s" % format)
+ send("from_multiple_%s" % format, data)
+ else
+ convert_from(format, data)
+ end
+ end
+
+ def render_multiple(format, instances)
+ if respond_to?("to_multiple_%s" % format)
+ send("to_multiple_%s" % format, instances)
+ else
+ instances.send("to_%s" % format)
+ end
+ end
+
def default_format
supported_formats[0]
end
diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb
index 3e0571eb6..4def67fef 100755
--- a/spec/unit/network/format_handler.rb
+++ b/spec/unit/network/format_handler.rb
@@ -8,18 +8,24 @@ class FormatTester
extend Puppet::Network::FormatHandler
# Not a supported format; missing the 'to'
- def self.from_nothing
- end
+ def self.from_nothing; end
# Not a supported format; missing the 'from'
- def to_nowhere
- end
+ def to_nowhere; end
- def self.from_good
- end
+ # A largely functional format.
+ def self.from_good; end
- def to_good
- end
+ def to_good; end
+
+ # A format that knows how to handle multiple instances specially.
+ def self.from_mults; end
+
+ def self.from_multiple_mults; end
+
+ def self.to_multiple_mults; end
+
+ def to_mults; end
end
describe Puppet::Network::FormatHandler do
@@ -53,12 +59,33 @@ describe Puppet::Network::FormatHandler do
FormatTester.convert_from(:good, "mydata")
end
+ it "should be able to use a specific hook for converting into multiple instances" do
+ FormatTester.expects(:from_multiple_mults).with("mydata")
+ FormatTester.convert_from_multiple(:mults, "mydata")
+ end
+
+ it "should default to the normal conversion method when no special method is available" do
+ FormatTester.expects(:from_good).with("mydata")
+ FormatTester.convert_from_multiple(:good, "mydata")
+ end
+
+ it "should be able to use a specific hook for rendering multiple instances" do
+ FormatTester.expects(:to_multiple_mults).with("mydata")
+ FormatTester.render_multiple(:mults, "mydata")
+ end
+
+ it "should use the instance method if no multiple-render hook is available" do
+ instances = mock 'instances'
+ instances.expects(:to_good)
+ FormatTester.render_multiple(:good, instances)
+ 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}
+ FormatTester.supported_formats.should == %w{good mults}
end
it "should return the first format as the default format" do