summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-04-25 21:07:20 +0100
committerJames Turnbull <james@lovedthanlost.net>2009-06-06 19:53:35 +1000
commit7666597edc335055275569f5626a897453402d5b (patch)
tree047ebd563f1044eba40bd6d6690b63988c166d7a
parentd40068f7e70d1dc09334cac34d1bc48579b5a717 (diff)
downloadpuppet-7666597edc335055275569f5626a897453402d5b.tar.gz
puppet-7666597edc335055275569f5626a897453402d5b.tar.xz
puppet-7666597edc335055275569f5626a897453402d5b.zip
Allowing formats to specify the individual method names to use
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/network/format.rb50
-rwxr-xr-xspec/unit/network/format.rb8
2 files changed, 35 insertions, 23 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index d50141ea9..5ed120ba6 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -6,39 +6,47 @@ require 'puppet/provider/confiner'
class Puppet::Network::Format
include Puppet::Provider::Confiner
- attr_reader :name, :mime, :weight, :required_methods
+ attr_reader :name, :mime
+ attr_accessor :intern_method, :render_method, :intern_multiple_method, :render_multiple_method, :weight, :required_methods
+
+ def init_attribute(name, default)
+ if value = @options[name]
+ @options.delete(name)
+ else
+ value = default
+ end
+ self.send(name.to_s + "=", value)
+ end
def initialize(name, options = {}, &block)
@name = name.to_s.downcase.intern
+ @options = options
+
# This must be done early the values can be used to set required_methods
define_method_names()
- if mime = options[:mime]
- self.mime = mime
- options.delete(:mime)
- else
- self.mime = "text/%s" % name
- end
+ method_list = {
+ :intern_method => "from_%s" % name,
+ :intern_multiple_method => "from_multiple_%s" % name,
+ :render_multiple_method => "to_multiple_%s" % name,
+ :render_method => "to_%s" % name
+ }
- if weight = options[:weight]
- @weight = weight
- options.delete(:weight)
- else
- @weight = 5
- end
+ init_attribute(:mime, "text/%s" % name)
+ init_attribute(:weight, 5)
+ init_attribute(:required_methods, method_list.keys)
- if methods = options[:required_methods]
- @required_methods = methods
- options.delete(:required_methods)
- else
- @required_methods = [:intern_method, :intern_multiple_method, :render_multiple_method, :render_method]
+ method_list.each do |method, value|
+ init_attribute(method, value)
end
- unless options.empty?
- raise ArgumentError, "Unsupported option(s) %s" % options.keys
+ unless @options.empty?
+ raise ArgumentError, "Unsupported option(s) %s" % @options.keys
end
+ @options = nil
+
instance_eval(&block) if block_given?
end
@@ -87,8 +95,6 @@ class Puppet::Network::Format
private
- attr_reader :intern_method, :render_method, :intern_multiple_method, :render_multiple_method
-
def define_method_names
@intern_method = "from_%s" % name
@render_method = "to_%s" % name
diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb
index 3e5045345..e5a6b599c 100755
--- a/spec/unit/network/format.rb
+++ b/spec/unit/network/format.rb
@@ -71,7 +71,7 @@ describe Puppet::Network::Format do
end
it "should default to its required methods being the individual and multiple methods for rendering and interning" do
- Puppet::Network::Format.new(:foo).required_methods.should == [:intern_method, :intern_multiple_method, :render_multiple_method, :render_method]
+ Puppet::Network::Format.new(:foo).required_methods.sort { |a,b| a.to_s <=> b.to_s }.should == [:intern_method, :intern_multiple_method, :render_multiple_method, :render_method].sort { |a,b| a.to_s <=> b.to_s }
end
it "should consider a class supported if the provided class has all required methods present" do
@@ -122,6 +122,12 @@ describe Puppet::Network::Format do
it "should be able to override its weight at initialization" do
Puppet::Network::Format.new(:foo, :weight => 1).weight.should == 1
end
+
+ [:intern_method, :intern_multiple_method, :render_multiple_method, :render_method].each do |method|
+ it "should allow assignment of the #{method}" do
+ Puppet::Network::Format.new(:foo, method => :foo).send(method).should == :foo
+ end
+ end
end
describe "when converting between instances and formatted text" do