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
Source
# File lib/facter/custom_facts/util/resolution.rb, line 67 def initialize(name, fact) @name = name @fact = fact @confines = [] @value = nil @timeout = 0 @weight = nil end
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
Public Instance Methods
Source
# File lib/facter/custom_facts/util/resolution.rb, line 161 def <=>(other) if weight == other.weight compare_equal_weights(other) else weight <=> other.weight end end
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
Source
# File lib/facter/custom_facts/util/resolution.rb, line 91 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) @last_evaluated = block.source_location.join(':') 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
Source
# File lib/facter/custom_facts/util/resolution.rb, line 109 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
Sets options for the aggregate fact
@return [nil]
@api private
Source
# File lib/facter/custom_facts/util/resolution.rb, line 81 def resolution_type :simple end
Returns the fact’s resolution type
@return [Symbol] The fact’s type
@api private
Source
# File lib/facter/custom_facts/util/resolution.rb, line 136 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
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
Private Instance Methods
Source
# File lib/facter/custom_facts/util/resolution.rb, line 176 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
If the weights are equal, we consider external facts greater tan custom facts
Source
# File lib/facter/custom_facts/util/resolution.rb, line 171 def log @log ||= Facter::Log.new(self) end
Source
# File lib/facter/custom_facts/util/resolution.rb, line 187 def resolve_value if !@value.nil? @value elsif @code.nil? nil elsif @code @code.call end end