diff options
author | Luke Kanies <luke@madstop.com> | 2008-07-30 11:00:15 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-07-30 11:00:15 -0500 |
commit | 3405841140ad118a500f44b6d35c9977b5eca473 (patch) | |
tree | c7606eca0da9a20b19160744b4370c727c847132 /lib | |
parent | 43a6911f656178efb5c2236c8b890127ab0880d3 (diff) | |
download | puppet-3405841140ad118a500f44b6d35c9977b5eca473.tar.gz puppet-3405841140ad118a500f44b6d35c9977b5eca473.tar.xz puppet-3405841140ad118a500f44b6d35c9977b5eca473.zip |
Moving functionality out of the FormatHandler into the Format class.
The Format class now takes care of deciding whether it's supported,
and it also does method dispatch to classes and instances as necessary.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/network/format.rb | 37 | ||||
-rw-r--r-- | lib/puppet/network/format_handler.rb | 46 |
2 files changed, 66 insertions, 17 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb index 7c678568d..832697836 100644 --- a/lib/puppet/network/format.rb +++ b/lib/puppet/network/format.rb @@ -23,5 +23,42 @@ class Puppet::Network::Format 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 + end + + def intern_multiple(klass, text) + return klass.send(intern_multiple_method, text) if klass.respond_to?(intern_multiple_method) + raise NotImplementedError + end + + def render(instance) + return instance.send(render_method) if instance.respond_to?(render_method) + raise NotImplementedError 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 + end + + def supported?(klass) + 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) + end + + private + + attr_reader :intern_method, :render_method, :intern_multiple_method, :render_multiple_method end diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb index 7e540708d..e42cc4c46 100644 --- a/lib/puppet/network/format_handler.rb +++ b/lib/puppet/network/format_handler.rb @@ -1,3 +1,4 @@ +require 'yaml' require 'puppet/network' require 'puppet/network/format' @@ -23,31 +24,34 @@ module Puppet::Network::FormatHandler @formats[name] end + # Provide a list of all formats. + def self.formats + @formats.keys + end + # Return a format capable of handling the provided mime type. def self.mime(mimetype) @formats.values.find { |format| format.mime == mimetype } end module ClassMethods + def format_handler + Puppet::Network::FormatHandler + end + def convert_from(format, data) raise ArgumentError, "Format %s not supported" % format unless support_format?(format) - send("from_%s" % format, data) + format_handler.format(format).intern(self, 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 + raise ArgumentError, "Format %s not supported" % format unless support_format?(format) + format_handler.format(format).intern_multiple(self, data) 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 + raise ArgumentError, "Format %s not supported" % format unless support_format?(format) + format_handler.format(format).render_multiple(instances) end def default_format @@ -55,15 +59,19 @@ module Puppet::Network::FormatHandler end def support_format?(name) - respond_to?("from_%s" % name) and instance_methods.include?("to_%s" % name) + Puppet::Network::FormatHandler.format(name).supported?(self) end def supported_formats - instance = instance_methods.collect { |m| m =~ /^to_(.+)$/ and $1 }.compact - klass = methods.collect { |m| m =~ /^from_(.+)$/ and $1 }.compact + format_handler.formats.collect { |f| format_handler.format(f) }.find_all { |f| f.supported?(self) }.collect { |f| f.name } + end - # Return the intersection of the two lists. - return instance & klass + def from_marshal(text) + Marshal.load(text) + end + + def from_yaml(text) + YAML.load(text) end end @@ -75,12 +83,16 @@ module Puppet::Network::FormatHandler format = self.class.default_format end - send("to_%s" % format) + Puppet::Network::FormatHandler.format(format).render(self) end def support_format?(name) self.class.support_format?(name) end + + def to_marshal(instance) + Marshal.dump(instance) + end end end |