class Facter::Util::Resolution

Attributes

code[RW]

@api private

fact[R]

@!attribute [r] fact

@return [Facter::Util::Fact] Associated fact with this resolution.

@api private

fact_type[RW]

@api private

file[R]

@!attribute [r] fact

@return [Facter::Util::Fact] Associated fact with this resolution.

@api private

last_evaluated[R]

@!attribute [r] fact

@return [Facter::Util::Fact] Associated fact with this resolution.

@api private

name[RW]

@!attribute [rw] name The name of this resolution. The resolution name should be unique with

respect to the given fact.

@return [String]

@api public

value[W]

@api private

Public Class Methods

new(name, fact) click to toggle source

Create a new resolution mechanism.

@param name [String] The name of the resolution.

@return [Facter::Util::Resolution] The created resolution

@api public

# File lib/facter/custom_facts/util/resolution.rb, line 63
def initialize(name, fact)
  @name = name
  @fact = fact
  @confines = []
  @value = nil
  @timeout = 0
  @weight = nil
end

Public Instance Methods

<=>(other) click to toggle source

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 164
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(&block) click to toggle source

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 87
def evaluate(&block)
  if @last_evaluated
    msg = "Already evaluated #{@name}"
    msg << " at #{@last_evaluated}" if msg.is_a? String
    msg << ', reevaluating anyways'
    LegacyFacter.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
options(options) click to toggle source

Sets options for the aggregate fact

@return [nil]

@api private

# File lib/facter/custom_facts/util/resolution.rb, line 112
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
resolution_type() click to toggle source

Returns the fact's resolution type

@return [Symbol] The fact's type

@api private

# File lib/facter/custom_facts/util/resolution.rb, line 77
def resolution_type
  :simple
end
setcode(string = nil, &block) click to toggle source

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 139
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

compare_equal_weights(other) click to toggle source

If the weights are equal, we consider external facts greater tan custom facts

# File lib/facter/custom_facts/util/resolution.rb, line 173
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
resolve_value() click to toggle source
# File lib/facter/custom_facts/util/resolution.rb, line 184
def resolve_value
  if @value
    @value
  elsif @code.nil?
    nil
  elsif @code
    @code.call
  end
end