class Facter::Util::Collection

Manage which facts exist and how we access them. Largely just a wrapper around a hash of facts.

Public Instance Methods

[](name) click to toggle source

Return a fact object by name. If you use this, you still have to call ‘value’ on it to retrieve the actual value.

# File lib/facter/util/collection.rb, line 10
def [](name)
  value(name)
end
add(name, options = {}, &block) click to toggle source

Add a resolution mechanism for a named fact. This does not distinguish between adding a new fact and adding a new way to resolve a fact.

# File lib/facter/util/collection.rb, line 16
def add(name, options = {}, &block)
  name = canonize(name)

  unless fact = @facts[name]
    fact = Facter::Util::Fact.new(name)

    @facts[name] = fact
  end

  # Set any fact-appropriate options.
  options.each do |opt, value|
    method = opt.to_s + "="
    if fact.respond_to?(method)
      fact.send(method, value)
      options.delete(opt)
    end
  end

  if block_given? and resolve = fact.add(&block)
    # If the resolve was actually added, set any resolve-appropriate options
    options.each do |opt, value|
      method = opt.to_s + "="
      if resolve.respond_to?(method)
        resolve.send(method, value)
        options.delete(opt)
      end
    end
  end

  unless options.empty?
    raise ArgumentError, "Invalid facter option(s) %s" % options.keys.collect { |k| k.to_s }.join(",")
  end

  return fact
end
each() { |name, value| ... } click to toggle source

Iterate across all of the facts.

# File lib/facter/util/collection.rb, line 55
def each
  @facts.each do |name, fact|
    value = fact.value
    unless value.nil?
      yield name.to_s, value
    end
  end
end
fact(name) click to toggle source

Return a fact by name.

# File lib/facter/util/collection.rb, line 65
def fact(name)
  name = canonize(name)

  # Try to load the fact if necessary
  loader.load(name) unless @facts[name]

  # Try HARDER
  loader.load_all unless @facts[name]

  @facts[name]
end
flush() click to toggle source

Flush all cached values.

# File lib/facter/util/collection.rb, line 78
def flush
  @facts.each { |name, fact| fact.flush }
end
list() click to toggle source

Return a list of all of the facts.

# File lib/facter/util/collection.rb, line 87
def list
  return @facts.keys
end
load_all() click to toggle source

Load all known facts.

# File lib/facter/util/collection.rb, line 92
def load_all
  loader.load_all
end
loader() click to toggle source

The thing that loads facts if we don’t have them.

# File lib/facter/util/collection.rb, line 97
def loader
  unless defined?(@loader)
    @loader = Facter::Util::Loader.new
  end
  @loader
end
to_hash() click to toggle source

Return a hash of all of our facts.

# File lib/facter/util/collection.rb, line 105
def to_hash
  @facts.inject({}) do |h, ary|
    value = ary[1].value
    if ! value.nil?
      # For backwards compatibility, convert the fact name to a string.
      h[ary[0].to_s] = value
    end
    h
  end
end
value(name) click to toggle source
# File lib/facter/util/collection.rb, line 116
def value(name)
  if fact = fact(name)
    fact.value
  end
end

Public Class Methods

new() click to toggle source
# File lib/facter/util/collection.rb, line 82
def initialize
  @facts = Hash.new
end