class Facter::Util::Fact

Constants

TIMEOUT

Attributes

ldapname[RW]
name[RW]

Public Instance Methods

add(&block) click to toggle source

Add a new resolution mechanism. This requires a block, which will then be evaluated in the context of the new mechanism.

# File lib/facter/util/fact.rb, line 33
def add(&block)
  raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given?

  begin
    resolve = Facter::Util::Resolution.new(@name)

    resolve.instance_eval(&block)

    @resolves << resolve

    # Immediately sort the resolutions, so that we always have
    # a sorted list for looking up values.
    @resolves.sort! { |a, b| b.weight <=> a.weight }

    resolve
  rescue => e
    Facter.warn "Unable to add resolve for #{@name}: #{e}"
    nil
  end
end
flush() click to toggle source

Flush any cached values.

# File lib/facter/util/fact.rb, line 55
def flush
  @value = nil
  @suitable = nil
end
value() click to toggle source

Return the value for a given fact. Searches through all of the mechanisms and returns either the first value or nil.

# File lib/facter/util/fact.rb, line 62
def value
  return @value if @value

  if @resolves.length == 0
    Facter.debug "No resolves for %s" % @name
    return nil
  end

  searching do
    @value = nil

    foundsuits = false
    @value = @resolves.inject(nil) { |result, resolve|
      next unless resolve.suitable?
      foundsuits = true

      tmp = resolve.value

      break tmp unless tmp.nil? or tmp == ""
    }

    unless foundsuits
      Facter.debug "Found no suitable resolves of %s for %s" % [@resolves.length, @name]
    end
  end

  if @value.nil?
    # nothing
    Facter.debug("value for %s is still nil" % @name)
    return nil
  else
    return @value
  end
end

Public Class Methods

new(name, options = {}) click to toggle source

Create a new fact, with no resolution mechanisms.

# File lib/facter/util/fact.rb, line 10
def initialize(name, options = {})
  @name = name.to_s.downcase.intern

  # LAK:NOTE: This is slow for many options, but generally we won't have any and at
  # worst we'll have one.  If we add more, this should be made more efficient.
  options.each do |name, value|
    case name
    when :ldapname; self.ldapname = value
    else
      raise ArgumentError, "Invalid fact option '%s'" % name
    end
  end

  @ldapname ||= @name.to_s

  @resolves = []
  @searching = false

  @value = nil
end