summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-07-09 19:07:30 -0700
committerLuke Kanies <luke@madstop.com>2008-07-29 00:47:01 -0500
commit0e7e16d19b12cf7748215d3dd3adf5190fb90d20 (patch)
treee075c5811cc915546e79b91697247a97ddd20500 /lib/puppet/network
parent40375a8fc34dbd85d87f507ba72c7394b25b7271 (diff)
downloadpuppet-0e7e16d19b12cf7748215d3dd3adf5190fb90d20.tar.gz
puppet-0e7e16d19b12cf7748215d3dd3adf5190fb90d20.tar.xz
puppet-0e7e16d19b12cf7748215d3dd3adf5190fb90d20.zip
Adding a FormatHandler module for managing format conversions.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/network')
-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