summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/network/format_handler.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
new file mode 100644
index 000000000..a69c29232
--- /dev/null
+++ b/lib/puppet/network/format_handler.rb
@@ -0,0 +1,33 @@
+require 'puppet/network'
+
+module Puppet::Network::FormatHandler
+ def self.extended(klass)
+ klass.extend(ClassMethods)
+
+ # LAK:NOTE This won't work in 1.9 ('send' won't be able to send
+ # private methods, but I don't know how else to do it.
+ klass.send(:include, InstanceMethods)
+ end
+
+ module ClassMethods
+ def convert_from(format, data)
+ raise ArgumentError, "Format %s not supported" % format unless support_format?(format)
+ send("from_%s" % format, data)
+ end
+
+ def support_format?(name)
+ respond_to?("from_%s" % name) and instance_methods.include?("to_%s" % name)
+ end
+ end
+
+ module InstanceMethods
+ def render(format)
+ raise ArgumentError, "Format %s not supported" % format unless support_format?(format)
+ send("to_%s" % format)
+ end
+
+ def support_format?(name)
+ self.class.support_format?(name)
+ end
+ end
+end