summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-08-24 14:10:39 -0500
committerLuke Kanies <luke@madstop.com>2008-08-26 22:40:40 -0700
commit90e70227b0bb7cfd104ae34de8f7c2b7250edb09 (patch)
treed94b5b375b5ea4a0532ef49dafd00eab5569d85c
parent5a195e0c06daa2bfa008cfd94c660e50b9d0ae56 (diff)
Adding weights to network formats, and sorting them based on the weight.
This way the new hackish RAW format will only ever be used if it's specifically chosen. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/network/format.rb9
-rw-r--r--lib/puppet/network/format_handler.rb5
-rw-r--r--lib/puppet/network/formats.rb3
-rwxr-xr-xspec/unit/network/format.rb12
-rwxr-xr-xspec/unit/network/format_handler.rb31
-rwxr-xr-xspec/unit/network/formats.rb4
6 files changed, 52 insertions, 12 deletions
diff --git a/lib/puppet/network/format.rb b/lib/puppet/network/format.rb
index 3e0a7d8aa..21aead7cc 100644
--- a/lib/puppet/network/format.rb
+++ b/lib/puppet/network/format.rb
@@ -5,7 +5,7 @@ require 'puppet/provider/confiner'
class Puppet::Network::Format
include Puppet::Provider::Confiner
- attr_reader :name, :mime
+ attr_reader :name, :mime, :weight
def initialize(name, options = {}, &block)
@name = name.to_s.downcase.intern
@@ -17,6 +17,13 @@ class Puppet::Network::Format
self.mime = "text/%s" % name
end
+ if weight = options[:weight]
+ @weight = weight
+ options.delete(:weight)
+ else
+ @weight = 5
+ end
+
unless options.empty?
raise ArgumentError, "Unsupported option(s) %s" % options.keys
end
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb
index 4c9f4e59e..f3c3380e1 100644
--- a/lib/puppet/network/format_handler.rb
+++ b/lib/puppet/network/format_handler.rb
@@ -61,7 +61,10 @@ module Puppet::Network::FormatHandler
end
def supported_formats
- format_handler.formats.collect { |f| format_handler.format(f) }.find_all { |f| f.supported?(self) }.collect { |f| f.name }
+ format_handler.formats.collect { |f| format_handler.format(f) }.find_all { |f| f.supported?(self) }.collect { |f| f.name }.sort do |a, b|
+ # It's an inverse sort -- higher weight formats go first.
+ format_handler.format(b).weight <=> format_handler.format(a).weight
+ end
end
end
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index c2a8b7ab6..85e8ce6f8 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -55,7 +55,8 @@ end
Puppet::Network::FormatHandler.create(:s, :mime => "text/plain")
-Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw") do
+# A very low-weight format so it'll never get chosen automatically.
+Puppet::Network::FormatHandler.create(:raw, :mime => "application/x-raw", :weight => 1) do
def intern_multiple(klass, text)
raise NotImplementedError
end
diff --git a/spec/unit/network/format.rb b/spec/unit/network/format.rb
index 818ecdce3..244bff306 100755
--- a/spec/unit/network/format.rb
+++ b/spec/unit/network/format.rb
@@ -83,6 +83,18 @@ describe Puppet::Network::Format do
@format.mime = "Foo/Bar"
@format.mime.should == "foo/bar"
end
+
+ it "should support having a weight" do
+ @format.should respond_to(:weight)
+ end
+
+ it "should default to a weight of of 5" do
+ @format.weight.should == 5
+ end
+
+ it "should be able to override its weight at initialization" do
+ Puppet::Network::Format.new(:foo, :weight => 1).weight.should == 1
+ end
end
describe "when converting between instances and formatted text" do
diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb
index 35e996516..3dcff6199 100755
--- a/spec/unit/network/format_handler.rb
+++ b/spec/unit/network/format_handler.rb
@@ -21,18 +21,31 @@ describe Puppet::Network::FormatHandler do
end
it "should include all supported formats" do
- one = stub 'supported', :supported? => true, :name => "one"
- two = stub 'supported', :supported? => false, :name => "two"
- three = stub 'supported', :supported? => true, :name => "three"
- four = stub 'supported', :supported? => false, :name => "four"
- Puppet::Network::FormatHandler.expects(:formats).returns %w{one two three four}
- Puppet::Network::FormatHandler.expects(:format).with("one").returns one
- Puppet::Network::FormatHandler.expects(:format).with("two").returns two
- Puppet::Network::FormatHandler.expects(:format).with("three").returns three
- Puppet::Network::FormatHandler.expects(:format).with("four").returns four
+ one = stub 'supported', :supported? => true, :name => "one", :weight => 1
+ two = stub 'supported', :supported? => false, :name => "two", :weight => 1
+ three = stub 'supported', :supported? => true, :name => "three", :weight => 1
+ four = stub 'supported', :supported? => false, :name => "four", :weight => 1
+ Puppet::Network::FormatHandler.stubs(:formats).returns %w{one two three four}
+ Puppet::Network::FormatHandler.stubs(:format).with("one").returns one
+ Puppet::Network::FormatHandler.stubs(:format).with("two").returns two
+ Puppet::Network::FormatHandler.stubs(:format).with("three").returns three
+ Puppet::Network::FormatHandler.stubs(:format).with("four").returns four
FormatTester.supported_formats.sort.should == %w{one three}.sort
end
+ it "should return the supported formats in decreasing order of weight" do
+ one = stub 'supported', :supported? => true, :name => "one", :weight => 1
+ two = stub 'supported', :supported? => true, :name => "two", :weight => 6
+ three = stub 'supported', :supported? => true, :name => "three", :weight => 2
+ four = stub 'supported', :supported? => true, :name => "four", :weight => 8
+ Puppet::Network::FormatHandler.stubs(:formats).returns %w{one two three four}
+ Puppet::Network::FormatHandler.stubs(:format).with("one").returns one
+ Puppet::Network::FormatHandler.stubs(:format).with("two").returns two
+ Puppet::Network::FormatHandler.stubs(:format).with("three").returns three
+ Puppet::Network::FormatHandler.stubs(:format).with("four").returns four
+ FormatTester.supported_formats.should == %w{four two three one}
+ end
+
it "should return the first format as the default format" do
FormatTester.expects(:supported_formats).returns %w{one two}
FormatTester.default_format.should == "one"
diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb
index 527fd9d79..0e21fefa7 100755
--- a/spec/unit/network/formats.rb
+++ b/spec/unit/network/formats.rb
@@ -124,5 +124,9 @@ describe "Puppet Network Format" do
it "should fail if its multiple_intern method is used" do
lambda { @format.intern_multiple(String, "foo") }.should raise_error(NotImplementedError)
end
+
+ it "should have a weight of 1" do
+ @format.weight.should == 1
+ end
end
end