summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Clamp <richardc@unixbeard.net>2011-03-18 00:10:00 +0000
committerJames Turnbull <james@lovedthanlost.net>2011-04-06 03:46:49 +1000
commitd56bca8534bd21c046fd19a7fb2f776fe3e100b4 (patch)
treee5af700326354cc68d80c645c6da1f934a074b7d
parent9f4c5c6ac79821700bf4e6beee81f3d865396f4b (diff)
downloadfacter-d56bca8534bd21c046fd19a7fb2f776fe3e100b4.tar.gz
facter-d56bca8534bd21c046fd19a7fb2f776fe3e100b4.tar.xz
facter-d56bca8534bd21c046fd19a7fb2f776fe3e100b4.zip
refactor the mechanism for allowing for resolution ordering to be influenced
renames Facter::Util::Resolution#length to weight as a more generic mechanism for allowing resolutions to state their importance
-rw-r--r--lib/facter/util/fact.rb4
-rw-r--r--lib/facter/util/loader.rb2
-rw-r--r--lib/facter/util/resolution.rb17
-rwxr-xr-xspec/unit/util/fact_spec.rb22
-rwxr-xr-xspec/unit/util/resolution_spec.rb22
5 files changed, 40 insertions, 27 deletions
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index e78ed97..935b3c1 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -41,9 +41,7 @@ class Facter::Util::Fact
# Immediately sort the resolutions, so that we always have
# a sorted list for looking up values.
- # We always want to look them up in the order of number of
- # confines, so the most restricted resolution always wins.
- @resolves.sort! { |a, b| b.length <=> a.length }
+ @resolves.sort! { |a, b| b.weight <=> a.weight }
return resolve
end
diff --git a/lib/facter/util/loader.rb b/lib/facter/util/loader.rb
index b6aa8de..a52012c 100644
--- a/lib/facter/util/loader.rb
+++ b/lib/facter/util/loader.rb
@@ -90,7 +90,7 @@ class Facter::Util::Loader
next if fact and env_name != fact
Facter.add($1) do
- from_environment
+ has_weight 1_000_000
setcode { value }
end
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index 2ca2447..d82fab2 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -85,9 +85,8 @@ class Facter::Util::Resolution
end
end
- # Say this resolution came from the environment
- def from_environment
- @from_environment = true
+ def has_weight(weight)
+ @weight = weight
end
# Create a new resolution mechanism.
@@ -96,15 +95,13 @@ class Facter::Util::Resolution
@confines = []
@value = nil
@timeout = 0
- @from_environment = false
+ @weight = nil
end
- # Return the number of confines.
- def length
- # If the resolution came from an environment variable
- # say we're very very sure about the value of the resolution
- if @from_environment
- 1_000_000_000
+ # Return the importance of this resolution.
+ def weight
+ if @weight
+ @weight
else
@confines.length
end
diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb
index db08670..523c855 100755
--- a/spec/unit/util/fact_spec.rb
+++ b/spec/unit/util/fact_spec.rb
@@ -56,10 +56,10 @@ describe Facter::Util::Fact do
@fact.add { }
end
- it "should re-sort the resolutions by length, so the most restricted resolutions are first" do
- r1 = stub 'r1', :length => 1
- r2 = stub 'r2', :length => 2
- r3 = stub 'r3', :length => 0
+ it "should re-sort the resolutions by weight, so the most restricted resolutions are first" do
+ r1 = stub 'r1', :weight => 1
+ r2 = stub 'r2', :weight => 2
+ r3 = stub 'r3', :weight => 0
Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
@fact.add { }
@fact.add { }
@@ -83,9 +83,9 @@ describe Facter::Util::Fact do
end
it "should return the first value returned by a resolution" do
- r1 = stub 'r1', :length => 2, :value => nil, :suitable? => true
- r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true
- r3 = stub 'r3', :length => 0, :value => "foo", :suitable? => true
+ r1 = stub 'r1', :weight => 2, :value => nil, :suitable? => true
+ r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
+ r3 = stub 'r3', :weight => 0, :value => "foo", :suitable? => true
Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
@fact.add { }
@fact.add { }
@@ -95,8 +95,8 @@ describe Facter::Util::Fact do
end
it "should short-cut returning the value once one is found" do
- r1 = stub 'r1', :length => 2, :value => "foo", :suitable? => true
- r2 = stub 'r2', :length => 1, :suitable? => true # would fail if 'value' were asked for
+ r1 = stub 'r1', :weight => 2, :value => "foo", :suitable? => true
+ r2 = stub 'r2', :weight => 1, :suitable? => true # would fail if 'value' were asked for
Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
@fact.add { }
@fact.add { }
@@ -105,8 +105,8 @@ describe Facter::Util::Fact do
end
it "should skip unsuitable resolutions" do
- r1 = stub 'r1', :length => 2, :suitable? => false # would fail if 'value' were asked for'
- r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true
+ r1 = stub 'r1', :weight => 2, :suitable? => false # would fail if 'value' were asked for'
+ r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
@fact.add { }
@fact.add { }
diff --git a/spec/unit/util/resolution_spec.rb b/spec/unit/util/resolution_spec.rb
index 581d0e1..3e13cdc 100755
--- a/spec/unit/util/resolution_spec.rb
+++ b/spec/unit/util/resolution_spec.rb
@@ -13,6 +13,10 @@ describe Facter::Util::Resolution do
Facter::Util::Resolution.new("yay").name.should == "yay"
end
+ it "should have a method for setting the weight" do
+ Facter::Util::Resolution.new("yay").should respond_to(:has_weight)
+ end
+
it "should have a method for setting the code" do
Facter::Util::Resolution.new("yay").should respond_to(:setcode)
end
@@ -195,11 +199,25 @@ describe Facter::Util::Resolution do
it "should provide a method for returning the number of confines" do
@resolve = Facter::Util::Resolution.new("yay")
@resolve.confine "one" => "foo", "two" => "fee"
- @resolve.length.should == 2
+ @resolve.weight.should == 2
end
it "should return 0 confines when no confines have been added" do
- Facter::Util::Resolution.new("yay").length.should == 0
+ Facter::Util::Resolution.new("yay").weight.should == 0
+ end
+
+ it "should provide a way to set the weight" do
+ @resolve = Facter::Util::Resolution.new("yay")
+ @resolve.has_weight(45)
+ @resolve.weight.should == 45
+ end
+
+ it "should allow the weight to override the number of confines" do
+ @resolve = Facter::Util::Resolution.new("yay")
+ @resolve.confine "one" => "foo", "two" => "fee"
+ @resolve.weight.should == 2
+ @resolve.has_weight(45)
+ @resolve.weight.should == 45
end
it "should have a method for determining if it is suitable" do