This is toward fixing #1943 - we need the ability to easily convert between file extensions and file formats.
Signed-off-by: Luke Kanies <[email protected]> --- lib/puppet/network/format.rb | 3 ++- lib/puppet/network/format_handler.rb | 7 +++++++ lib/puppet/network/formats.rb | 2 +- spec/unit/network/format.rb | 7 +++++++ spec/unit/network/format_handler.rb | 9 +++++++++ spec/unit/network/formats.rb | 4 ++++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb index a5be3af..048848c 100644 --- a/lib/puppet/network/format.rb +++ b/lib/puppet/network/format.rb @@ -7,7 +7,7 @@ class Puppet::Network::Format include Puppet::Provider::Confiner attr_reader :name, :mime - attr_accessor :intern_method, :render_method, :intern_multiple_method, :render_multiple_method, :weight, :required_methods + attr_accessor :intern_method, :render_method, :intern_multiple_method, :render_multiple_method, :weight, :required_methods, :extension def init_attribute(name, default) if value = @options[name] @@ -36,6 +36,7 @@ class Puppet::Network::Format init_attribute(:mime, "text/%s" % name) init_attribute(:weight, 5) init_attribute(:required_methods, method_list.keys) + init_attribute(:extension, name.to_s) method_list.each do |method, value| init_attribute(method, value) diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb index 2ffbcef..2c75271 100644 --- a/lib/puppet/network/format_handler.rb +++ b/lib/puppet/network/format_handler.rb @@ -51,6 +51,13 @@ module Puppet::Network::FormatHandler @formats[name.to_s.downcase.intern] end + def self.format_by_extension(ext) + @formats.each do |name, format| + return format if format.extension == ext + end + return nil + end + # Provide a list of all formats. def self.formats @formats.keys diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index df6ef39..3080629 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -66,7 +66,7 @@ Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal") do end end -Puppet::Network::FormatHandler.create(:s, :mime => "text/plain") +Puppet::Network::FormatHandler.create(:s, :mime => "text/plain", :extension => "txt") # A very low-weight format so it'll never get chosen automatically. Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw", :weight => 1) do diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb index e5a6b59..73530b9 100755 --- a/spec/unit/network/format.rb +++ b/spec/unit/network/format.rb @@ -123,6 +123,13 @@ describe Puppet::Network::Format do Puppet::Network::Format.new(:foo, :weight => 1).weight.should == 1 end + it "should default to its extension being equal to its name" do + Puppet::Network::Format.new(:foo).extension.should == "foo" + end + + it "should support overriding the extension" do + Puppet::Network::Format.new(:foo, :extension => "bar").extension.should == "bar" + end [:intern_method, :intern_multiple_method, :render_multiple_method, :render_method].each do |method| it "should allow assignment of the #{method}" do Puppet::Network::Format.new(:foo, method => :foo).send(method).should == :foo diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb index 8a79a58..d076b98 100755 --- a/spec/unit/network/format_handler.rb +++ b/spec/unit/network/format_handler.rb @@ -184,6 +184,15 @@ describe Puppet::Network::FormatHandler do Puppet::Network::FormatHandler.format(:by_name).should equal(format) end + it "should be able to retrieve a format by extension" do + format = Puppet::Network::FormatHandler.create(:by_extension, :extension => "foo") + Puppet::Network::FormatHandler.format_by_extension("foo").should equal(format) + end + + it "should return nil if asked to return a format by an unknown extension" do + Puppet::Network::FormatHandler.format_by_extension("yayness").should be_nil + end + it "should be able to retrieve formats by name irrespective of case and class" do format = Puppet::Network::FormatHandler.create(:by_name) Puppet::Network::FormatHandler.format(:By_Name).should equal(format) diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb index de2e0af..deca8e6 100755 --- a/spec/unit/network/formats.rb +++ b/spec/unit/network/formats.rb @@ -141,6 +141,10 @@ describe "Puppet Network Format" do it "should have its mimetype set to text/plain" do @text.mime.should == "text/plain" end + + it "should use 'txt' as its extension" do + @text.extension.should == "txt" + end end describe Puppet::Network::FormatHandler.format(:raw) do -- 1.6.1 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
