Class: Facter::Util::Resolution
- Inherits:
-
Object
- Object
- Facter::Util::Resolution
- Extended by:
- Core::Execution
- Includes:
- Core::Resolvable, Core::Suitable
- Defined in:
- lib/facter/util/resolution.rb
Overview
This represents a fact resolution. A resolution is a concrete implementation of a fact. A single fact can have many resolutions and the correct resolution will be chosen at runtime. Each time Facter.add is called, a new resolution is created and added to the set of resolutions for the fact named in the call. Each resolution has a weight, which defines its priority over other resolutions, and a set of confinements, which defines the conditions under which it will be chosen. All confinements must be satisfied for a fact to be considered suitable.
Instance Attribute Summary collapse
- #code ⇒ Object private
- #fact ⇒ Facter::Util::Fact readonly private
-
#name ⇒ String
The name of this resolution.
- #value ⇒ Object writeonly
Attributes included from Core::Suitable
Attributes included from Core::Resolvable
Instance Method Summary collapse
-
#evaluate(&block) ⇒ void
Evaluate the given block in the context of this resolution.
-
#initialize(name, fact) ⇒ void
constructor
private
Create a new resolution mechanism.
- #resolution_type ⇒ Object
- #set_options(options) ⇒ Object
-
#setcode(string = nil, &block) ⇒ void
Sets the code block or external program that will be evaluated to get the value of the fact.
Methods included from Core::Execution
absolute_path?, exec, execute, expand_command, impl, search_paths, which, with_env
Methods included from Core::Suitable
#confine, #has_weight, #suitable?
Methods included from Core::Resolvable
#flush, #limit, #on_flush, #value
Constructor Details
#initialize(name, fact) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new resolution mechanism.
56 57 58 59 60 61 62 63 |
# File 'lib/facter/util/resolution.rb', line 56 def initialize(name, fact) @name = name @fact = fact @confines = [] @value = nil @timeout = 0 @weight = nil end |
Instance Attribute Details
#code ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
21 22 23 |
# File 'lib/facter/util/resolution.rb', line 21 def code @code end |
#fact ⇒ Facter::Util::Fact (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
48 49 50 |
# File 'lib/facter/util/resolution.rb', line 48 def fact @fact end |
#name ⇒ String
The name of this resolution. The resolution name should be unique with respect to the given fact.
43 44 45 |
# File 'lib/facter/util/resolution.rb', line 43 def name @name end |
#value=(value) ⇒ Object (writeonly)
22 23 24 |
# File 'lib/facter/util/resolution.rb', line 22 def value=(value) @value = value end |
Instance Method Details
#evaluate(&block) ⇒ void
This method returns an undefined value.
Evaluate the given block in the context of this resolution. If a block has already been evaluated emit a warning to that effect.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/facter/util/resolution.rb', line 73 def evaluate(&block) if @last_evaluated msg = "Already evaluated #{@name}" msg << " at #{@last_evaluated}" if msg.is_a? String msg << ", reevaluating anyways" Facter.warn msg end instance_eval(&block) # Ruby 1.9+ provides the source location of procs which can provide useful # debugging information if a resolution is being evaluated twice. Since 1.8 # doesn't support this we opportunistically provide this information. if block.respond_to? :source_location @last_evaluated = block.source_location.join(':') else @last_evaluated = true end end |
#resolution_type ⇒ Object
65 66 67 |
# File 'lib/facter/util/resolution.rb', line 65 def resolution_type :simple end |
#set_options(options) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/facter/util/resolution.rb', line 93 def () if [:name] @name = .delete(:name) end if .has_key?(:value) @value = .delete(:value) end if .has_key?(:timeout) @timeout = .delete(:timeout) end if .has_key?(:weight) @weight = .delete(:weight) end if not .keys.empty? raise ArgumentError, "Invalid resolution options #{.keys.inspect}" end end |
#setcode(string) ⇒ void #setcode(&block) ⇒ void
This method returns an undefined value.
Sets the code block or external program that will be evaluated to get the value of the fact.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/facter/util/resolution.rb', line 132 def setcode(string = nil, &block) if string @code = Proc.new do output = Facter::Core::Execution.execute(string, :on_fail => nil) if output.nil? or output.empty? nil else output end end elsif block_given? @code = block else raise ArgumentError, "You must pass either code or a block" end end |