[Pkg-puppet-devel] [facter] 156/352: (FACT-65) Allow resolutions to be created and looked up by name

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:41 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 05ee7ce19d25658fd3783da87c6d374559526790
Author: Adrien Thebo <git at somethingsinistral.net>
Date:   Mon Dec 23 14:49:37 2013 -0800

    (FACT-65) Allow resolutions to be created and looked up by name
---
 lib/facter/util/fact.rb       | 36 +++++++++++++++++++++++++++++++++++-
 lib/facter/util/resolution.rb | 12 +++++++++---
 spec/unit/util/fact_spec.rb   | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index 1158253..845c1a2 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -67,7 +67,41 @@ class Facter::Util::Fact
     end
   end
 
-  # Flushs any cached values.
+  # Define a new named resolution or return an existing resolution with
+  # the given name.
+  #
+  # @param resolve_name [String] The name of the resolve to define or look up
+  # @return [void]
+  #
+  # @api public
+  def define_resolution(resolve_name, &block)
+    resolve = self.resolution(resolve_name)
+
+    if resolve.nil?
+      resolve = Facter::Util::Resolution.new(resolve_name)
+      resolve.instance_eval(&block) if block
+      @resolves << resolve
+    else
+      resolve.instance_eval(&block) if block
+    end
+
+  rescue => e
+    Facter.warn "Unable to add resolve #{resolve_name} for fact #{@name}: #{e}"
+  end
+
+  # Retrieve an existing resolution by name
+  #
+  # @param name [String]
+  #
+  # @return [Facter::Util::Resolution, nil] The resolution if exists, nil if
+  #   it doesn't exist or name is nil
+  def resolution(name)
+    return nil if name.nil?
+
+    @resolves.find { |resolve| resolve.name == name }
+  end
+
+  # Flushes any cached values.
   #
   # @return [void]
   #
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index c7e6b83..732bf68 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -24,8 +24,15 @@ class Facter::Util::Resolution
   # @api public
   attr_accessor :timeout
 
+  # @!attribute [rw] name
+  # The name of this resolution. The resolution name should be unique with
+  # respect to the given fact.
+  # @return [String]
+  # @api public
+  attr_accessor :name
+
   # @api private
-  attr_accessor :interpreter, :code, :name
+  attr_accessor :code
   attr_writer :value, :weight
 
   INTERPRETER = Facter::Util::Config.is_windows? ? "cmd.exe" : "/bin/sh"
@@ -86,8 +93,7 @@ class Facter::Util::Resolution
 
   # Create a new resolution mechanism.
   #
-  # @param name [String] The name of the resolution. This is mostly
-  #   unused and resolutions are treated as anonymous.
+  # @param name [String] The name of the resolution.
   # @return [void]
   #
   # @api private
diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb
index c094100..a22f3e9 100755
--- a/spec/unit/util/fact_spec.rb
+++ b/spec/unit/util/fact_spec.rb
@@ -52,6 +52,43 @@ describe Facter::Util::Fact do
     end
   end
 
+  describe "looking up resolutions by name" do
+    subject(:fact) { described_class.new('yay') }
+
+    it "returns nil if no such resolution exists" do
+      expect(fact.resolution('nope')).to be_nil
+    end
+
+    it "never returns anonymous resolutions" do
+      fact.add() { setcode { 'anonymous' } }
+
+      expect(fact.resolution(nil)).to be_nil
+    end
+  end
+
+  describe "adding resolution mechanisms by name" do
+    subject(:fact) { described_class.new('yay') }
+
+    it "creates a new resolution if no such resolution exists" do
+      res = stub 'resolution', :name => 'named'
+      Facter::Util::Resolution.expects(:new).once.with('named').returns(res)
+
+      fact.define_resolution('named')
+
+      expect(fact.resolution('named')).to eq res
+    end
+
+    it "returns existing resolutions by name" do
+      res = stub 'resolution', :name => 'named'
+      Facter::Util::Resolution.expects(:new).once.with('named').returns(res)
+
+      fact.define_resolution('named')
+      fact.define_resolution('named')
+
+      expect(fact.resolution('named')).to eq res
+    end
+  end
+
   it "should be able to return a value" do
     Facter::Util::Fact.new("yay").should respond_to(:value)
   end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list