summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-07-28 18:50:24 -0500
committerLuke Kanies <luke@madstop.com>2008-07-29 00:51:22 -0500
commit352e2d0fb74fd7226ecac06b6108e672c221baa1 (patch)
tree859bc5a7b73af3ac3e22a16dac68877636bd72df /lib/puppet
parent55e29444c15fe2f810b3d5332605f27ac5942fda (diff)
downloadpuppet-352e2d0fb74fd7226ecac06b6108e672c221baa1.tar.gz
puppet-352e2d0fb74fd7226ecac06b6108e672c221baa1.tar.xz
puppet-352e2d0fb74fd7226ecac06b6108e672c221baa1.zip
Adding rudimentary support for directly managing formats.
We have a simple Format class, and the FormatHandler module is responsible for managing its instances, including providing access by mime type or name. It will soon replace some of the testing functionality in the FormatHandler module, but for now, it's just there as a name => mimetype converter, which makes it seem a lot like overkill. Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/network/format.rb27
-rw-r--r--lib/puppet/network/format_handler.rb21
-rw-r--r--lib/puppet/network/formats.rb4
3 files changed, 52 insertions, 0 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
new file mode 100644
index 000000000..7c678568d
--- /dev/null
+++ b/lib/puppet/network/format.rb
@@ -0,0 +1,27 @@
+require 'puppet/provider/confiner'
+
+# A simple class for modeling encoding formats for moving
+# instances around the network.
+class Puppet::Network::Format
+ include Puppet::Provider::Confiner
+
+ attr_reader :name
+ attr_accessor :mime
+
+ def initialize(name, options = {}, &block)
+ @name = name
+
+ if mime = options[:mime]
+ @mime = mime
+ options.delete(:mime)
+ else
+ @mime = "text/%s" % name
+ end
+
+ unless options.empty?
+ raise ArgumentError, "Unsupported option(s) %s" % options.keys
+ end
+
+ instance_eval(&block) if block_given?
+ end
+end
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
index f001f8242..7e540708d 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -1,6 +1,16 @@
require 'puppet/network'
+require 'puppet/network/format'
module Puppet::Network::FormatHandler
+ @formats = {}
+ def self.create(*args, &block)
+ instance = Puppet::Network::Format.new(*args)
+ instance.instance_eval(&block) if block_given?
+
+ @formats[instance.name] = instance
+ instance
+ end
+
def self.extended(klass)
klass.extend(ClassMethods)
@@ -9,6 +19,15 @@ module Puppet::Network::FormatHandler
klass.send(:include, InstanceMethods)
end
+ def self.format(name)
+ @formats[name]
+ 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 convert_from(format, data)
raise ArgumentError, "Format %s not supported" % format unless support_format?(format)
@@ -64,3 +83,5 @@ module Puppet::Network::FormatHandler
end
end
end
+
+require 'puppet/network/formats'
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
new file mode 100644
index 000000000..640a24485
--- /dev/null
+++ b/lib/puppet/network/formats.rb
@@ -0,0 +1,4 @@
+require 'puppet/network/format_handler'
+
+Puppet::Network::FormatHandler.create(:yaml, :mime => "text/yaml")
+Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal")