class LegacyFacter::Util::Collection

Attributes

external_loader[R]
internal_loader[R]

Public Class Methods

new(internal_loader, external_loader) click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 9
def initialize(internal_loader, external_loader)
  @facts = {}
  @internal_loader = internal_loader
  @external_loader = external_loader
  @loaded = false
end

Public Instance Methods

[](name) click to toggle source

Return a fact object by name.

# File lib/facter/custom_facts/util/collection.rb, line 17
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.

@param name [Symbol] The name of the fact to define @param options [Hash] A hash of options to set on the fact and resolution

@return [Facter::Util::Fact] The fact that was defined

# File lib/facter/custom_facts/util/collection.rb, line 44
def add(name, options = {}, &block)
  fact = create_or_return_fact(name, options)

  fact.add(options, &block)

  fact
end
custom_facts() click to toggle source

Builds a hash of custom facts

# File lib/facter/custom_facts/util/collection.rb, line 109
def custom_facts
  return @custom_facts if @valid_custom_facts

  @valid_custom_facts = true

  internal_loader.load_all unless @loaded
  @loaded = true

  custom_facts = @facts.select { |_k, v| v.options[:fact_type] == :custom }
  @custom_facts = Facter::Utils.deep_copy(custom_facts.keys)
end
define_fact(name, options = {}, &block) click to toggle source

Define a new fact or extend an existing fact.

@param name [Symbol] The name of the fact to define @param options [Hash] A hash of options to set on the fact

@return [Facter::Util::Fact] The fact that was defined

# File lib/facter/custom_facts/util/collection.rb, line 27
def define_fact(name, options = {}, &block)
  fact = create_or_return_fact(name, options)

  fact.instance_eval(&block) if block_given?

  fact
rescue StandardError => e
  Facter.log_exception(e, "Unable to add fact #{name}: #{e}")
end
each() { |name, value| ... } click to toggle source

Iterate across all of the facts.

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

Build a hash of external facts

# File lib/facter/custom_facts/util/collection.rb, line 93
def external_facts
  return @external_facts unless @external_facts.nil?

  load_external_facts
  @external_facts = @facts.select { |_k, v| v.options[:fact_type] == :external }
end
fact(name) click to toggle source

Return a fact by name.

# File lib/facter/custom_facts/util/collection.rb, line 64
def fact(name)
  name = canonicalize(name)

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

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

  if @facts.empty?
    LegacyFacter.warnonce("No facts loaded from #{internal_loader.search_path.join(File::PATH_SEPARATOR)}")
  end

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

Flush all cached values.

# File lib/facter/custom_facts/util/collection.rb, line 81
def flush
  @facts.each { |_name, fact| fact.flush }
  @external_facts_loaded = nil
end
invalidate_custom_facts() click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 100
def invalidate_custom_facts
  @valid_custom_facts = false
end
list() click to toggle source

Return a list of all of the facts.

# File lib/facter/custom_facts/util/collection.rb, line 87
def list
  load_all
  @facts.keys
end
load(name) click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 121
def load(name)
  internal_loader.load(name)
  load_external_facts
end
load_all() click to toggle source

Load all known facts.

# File lib/facter/custom_facts/util/collection.rb, line 127
def load_all
  internal_loader.load_all
  load_external_facts
end
reload_custom_facts() click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 104
def reload_custom_facts
  @loaded = false
end
to_hash() click to toggle source

Return a hash of all of our facts.

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

Private Instance Methods

canonicalize(name) click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 169
def canonicalize(name)
  name.to_s.downcase.to_sym
end
create_or_return_fact(name, options) click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 154
def create_or_return_fact(name, options)
  name = canonicalize(name)

  fact = @facts[name]

  if fact.nil?
    fact = Facter::Util::Fact.new(name, options)
    @facts[name] = fact
  else
    fact.extract_ldapname_option!(options)
  end

  fact
end
load_external_facts() click to toggle source
# File lib/facter/custom_facts/util/collection.rb, line 173
def load_external_facts
  return if @external_facts_loaded

  @external_facts_loaded = true
  external_loader.load(self)
end