diff options
author | Luke Kanies <luke@madstop.com> | 2008-07-28 18:50:24 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-07-29 00:51:22 -0500 |
commit | 352e2d0fb74fd7226ecac06b6108e672c221baa1 (patch) | |
tree | 859bc5a7b73af3ac3e22a16dac68877636bd72df | |
parent | 55e29444c15fe2f810b3d5332605f27ac5942fda (diff) | |
download | puppet-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>
-rw-r--r-- | lib/puppet/network/format.rb | 27 | ||||
-rw-r--r-- | lib/puppet/network/format_handler.rb | 21 | ||||
-rw-r--r-- | lib/puppet/network/formats.rb | 4 | ||||
-rwxr-xr-x | spec/unit/network/format.rb | 38 | ||||
-rwxr-xr-x | spec/unit/network/format_handler.rb | 31 | ||||
-rwxr-xr-x | spec/unit/network/formats.rb | 27 |
6 files changed, 148 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") diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb new file mode 100755 index 000000000..78e677e9b --- /dev/null +++ b/spec/unit/network/format.rb @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/network/format' + +describe Puppet::Network::Format do + it "should require a name" do + lambda { Puppet::Network::Format.new }.should raise_error(ArgumentError) + end + + it "should be able to provide its name" do + Puppet::Network::Format.new(:my_format).name.should == :my_format + end + + it "should be able to set its mime type at initialization" do + format = Puppet::Network::Format.new(:my_format, :mime => "foo/bar") + format.mime.should == "foo/bar" + end + + it "should default to text plus the name of the format as the mime type" do + Puppet::Network::Format.new(:my_format).mime.should == "text/my_format" + end + + it "should fail if unsupported options are provided" do + lambda { Puppet::Network::Format.new(:my_format, :foo => "bar") }.should raise_error(ArgumentError) + end + + it "should support being confined" do + Puppet::Network::Format.new(:my_format).should respond_to(:confine) + end + + it "should not be considered suitable if confinement conditions are not met" do + format = Puppet::Network::Format.new(:my_format) + format.confine :true => false + format.should_not be_suitable + end +end diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb index 4def67fef..6e3033c75 100755 --- a/spec/unit/network/format_handler.rb +++ b/spec/unit/network/format_handler.rb @@ -93,6 +93,37 @@ describe Puppet::Network::FormatHandler do FormatTester.default_format.should == "one" end + describe "when managing formats" do + it "should have a method for defining a new format" do + Puppet::Network::FormatHandler.should respond_to(:create) + end + + it "should create a format instance when asked" do + format = stub 'format', :name => "foo" + Puppet::Network::Format.expects(:new).with(:foo).returns format + Puppet::Network::FormatHandler.create(:foo) + end + + it "should instance_eval any block provided when creating a format" do + format = stub 'format', :name => :instance_eval + format.expects(:yayness) + Puppet::Network::Format.expects(:new).returns format + Puppet::Network::FormatHandler.create(:instance_eval) do + yayness + end + end + + it "should be able to retrieve a format by name" do + format = Puppet::Network::FormatHandler.create(:by_name) + Puppet::Network::FormatHandler.format(:by_name).should equal(format) + end + + it "should be able to retrieve a format by mime type" do + format = Puppet::Network::FormatHandler.create(:by_name, :mime => "foo/bar") + Puppet::Network::FormatHandler.mime("foo/bar").should equal(format) + end + end + describe "when an instance" do it "should be able to test whether a format is supported" do FormatTester.new.should respond_to(:support_format?) diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb new file mode 100755 index 000000000..94104289d --- /dev/null +++ b/spec/unit/network/formats.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/network/formats' + +describe "Puppet Network Format" do + it "should include a yaml format" do + Puppet::Network::FormatHandler.format(:yaml).should_not be_nil + end + + describe "yaml" do + it "should have its mime type set to text/yaml" do + Puppet::Network::FormatHandler.format(:yaml).mime.should == "text/yaml" + end + end + + it "should include a marshal format" do + Puppet::Network::FormatHandler.format(:marshal).should_not be_nil + end + + describe "marshal" do + it "should have its mime type set to text/marshal" do + Puppet::Network::FormatHandler.format(:marshal).mime.should == "text/marshal" + end + end +end |