class Facter::Util::Resolution
Attributes
@api private
@!attribute [r] fact
@return [Facter::Util::Fact] Associated fact with this resolution.
@api private
@api private
@!attribute [r] fact
@return [Facter::Util::Fact] Associated fact with this resolution.
@api private
@!attribute [r] fact
@return [Facter::Util::Fact] Associated fact with this resolution.
@api private
@!attribute [rw] name The name of this resolution. The resolution name should be unique with
respect to the given fact.
@return [String]
@api public
@api private
Public Class Methods
Create a new resolution mechanism.
@param name [String] The name of the resolution. @param fact [Facter::Fact] The fact to which this
resolution will be added.
@return [Facter::Util::Resolution] The created resolution
@api public
# File lib/facter/custom_facts/util/resolution.rb, line 65 def initialize(name, fact) @name = name @fact = fact @confines = [] @value = nil @timeout = 0 @weight = nil end
Public Instance Methods
Comparison is done based on weight and fact type.
The greater the weight, the higher the priority. If weights are equal, we consider external facts greater than custom facts.
@return [bool] Weight comparison result
@api private
# File lib/facter/custom_facts/util/resolution.rb, line 166 def <=>(other) return compare_equal_weights(other) if weight == other.weight return 1 if weight > other.weight return -1 if weight < other.weight end
Evaluate the given block in the context of this resolution. If a block has already been evaluated emit a warning to that effect.
@return [String] Result of the block's evaluation
@api private
# File lib/facter/custom_facts/util/resolution.rb, line 89 def evaluate(&block) if @last_evaluated msg = "Already evaluated #{@name}" msg << " at #{@last_evaluated}" if msg.is_a? String msg << ', reevaluating anyways' log.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. @last_evaluated = if block.respond_to? :source_location block.source_location.join(':') else true end end
Sets options for the aggregate fact
@return [nil]
@api private
# File lib/facter/custom_facts/util/resolution.rb, line 114 def options(options) accepted_options = %i[name value timeout weight fact_type file is_env] accepted_options.each do |option_name| instance_variable_set("@#{option_name}", options.delete(option_name)) if options.key?(option_name) end raise ArgumentError, "Invalid resolution options #{options.keys.inspect}" unless options.keys.empty? end
Returns the fact's resolution type
@return [Symbol] The fact's type
@api private
# File lib/facter/custom_facts/util/resolution.rb, line 79 def resolution_type :simple end
Sets the code block or external program that will be evaluated to get the value of the fact.
@overload setcode(string)
Sets an external program to call to get the value of the resolution @param [String] string the external program to run to get the value
@overload setcode(&block)
Sets the resolution's value by evaluating a block at runtime @param [Proc] block The block to determine the resolution's value. This block is run when the fact is evaluated. Errors raised from inside the block are rescued and printed to stderr.
@return [Facter::Util::Resolution] Returns itself
@api public
# File lib/facter/custom_facts/util/resolution.rb, line 141 def setcode(string = nil, &block) if string @code = proc do output = Facter::Core::Execution.execute(string, on_fail: nil) if output.nil? || output.empty? nil else output end end elsif block_given? @code = block else raise ArgumentError, 'You must pass either code or a block' end self end
Private Instance Methods
If the weights are equal, we consider external facts greater tan custom facts
# File lib/facter/custom_facts/util/resolution.rb, line 179 def compare_equal_weights(other) # Other is considered greater because self is custom fact and other is external return -1 if fact_type == :custom && other.fact_type == :external # Self is considered greater, because it is external fact and other is custom return 1 if fact_type == :external && other.fact_type == :custom # They are considered equal 0 end
# File lib/facter/custom_facts/util/resolution.rb, line 174 def log @log ||= Facter::Log.new(self) end
# File lib/facter/custom_facts/util/resolution.rb, line 190 def resolve_value if !@value.nil? @value elsif @code.nil? nil elsif @code @code.call end end