diff options
author | Luke Kanies <luke@madstop.com> | 2009-04-25 20:55:22 +0100 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-06-06 19:53:34 +1000 |
commit | d40068f7e70d1dc09334cac34d1bc48579b5a717 (patch) | |
tree | 07931d17146315b7c58e0b5cac494eee156d9d00 /lib/puppet | |
parent | 024ccf5b9a30977e9eee4ecf1d91eaf64f44a509 (diff) | |
download | puppet-d40068f7e70d1dc09334cac34d1bc48579b5a717.tar.gz puppet-d40068f7e70d1dc09334cac34d1bc48579b5a717.tar.xz puppet-d40068f7e70d1dc09334cac34d1bc48579b5a717.zip |
Allowing formats to specify the methods they require
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/network/format.rb | 67 |
1 files changed, 52 insertions, 15 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb index db9458ee3..d50141ea9 100644 --- a/lib/puppet/network/format.rb +++ b/lib/puppet/network/format.rb @@ -6,11 +6,14 @@ require 'puppet/provider/confiner' class Puppet::Network::Format include Puppet::Provider::Confiner - attr_reader :name, :mime, :weight + attr_reader :name, :mime, :weight, :required_methods def initialize(name, options = {}, &block) @name = name.to_s.downcase.intern + # 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) @@ -25,26 +28,28 @@ class Puppet::Network::Format @weight = 5 end + 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] + end + unless options.empty? raise ArgumentError, "Unsupported option(s) %s" % options.keys end instance_eval(&block) if block_given? - - @intern_method = "from_%s" % name - @render_method = "to_%s" % name - @intern_multiple_method = "from_multiple_%s" % name - @render_multiple_method = "to_multiple_%s" % name end def intern(klass, text) return klass.send(intern_method, text) if klass.respond_to?(intern_method) - raise NotImplementedError, "%s can not intern instances from %s" % [klass, mime] + raise NotImplementedError, "%s does not respond to %s; can not intern instances from %s" % [klass, intern_method, mime] end def intern_multiple(klass, text) return klass.send(intern_multiple_method, text) if klass.respond_to?(intern_multiple_method) - raise NotImplementedError, "%s can not intern multiple instances from %s" % [klass, mime] + raise NotImplementedError, "%s does not respond to %s; can not intern multiple instances from %s" % [klass, intern_multiple_method, mime] end def mime=(mime) @@ -53,21 +58,27 @@ class Puppet::Network::Format def render(instance) return instance.send(render_method) if instance.respond_to?(render_method) - raise NotImplementedError, "%s can not render instances to %s" % [instance.class, mime] + raise NotImplementedError, "%s does not respond to %s; can not render instances to %s" % [instance.class, render_method, mime] end def render_multiple(instances) # This method implicitly assumes that all instances are of the same type. return instances[0].class.send(render_multiple_method, instances) if instances[0].class.respond_to?(render_multiple_method) - raise NotImplementedError, "%s can not intern multiple instances to %s" % [instances[0].class, mime] + raise NotImplementedError, "%s does not respond to %s; can not intern multiple instances to %s" % [instances[0].class, render_multiple_method, mime] + end + + def required_methods_present?(klass) + [:intern_method, :intern_multiple_method, :render_multiple_method].each do |name| + return false unless required_method_present?(name, klass, :class) + end + + return false unless required_method_present?(:render_method, klass, :instance) + + return true end def supported?(klass) - suitable? and - klass.respond_to?(intern_method) and - klass.respond_to?(intern_multiple_method) and - klass.respond_to?(render_multiple_method) and - klass.instance_methods.include?(render_method) + suitable? and required_methods_present?(klass) end def to_s @@ -77,4 +88,30 @@ 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 + @intern_multiple_method = "from_multiple_%s" % name + @render_multiple_method = "to_multiple_%s" % name + end + + def required_method_present?(name, klass, type) + return true unless required_methods.include?(name) + + method = send(name) + + if type == :class + has_method = klass.respond_to?(method) + message = "class does not respond to %s" % method + else + has_method = klass.instance_methods.include?(method) + message = "class instances do not respond to %s" % method + end + + return true if has_method + + Puppet.debug "Format %s not supported for %s; %s" % [name, klass, message] + return false + end end |