class Facter::FactCollection

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/facter/models/fact_collection.rb, line 5
def initialize
  super
  @log = Log.new(self)
end

Public Instance Methods

build_fact_collection!(facts) click to toggle source

Transorms a list of {Facter::ResolvedFact} into a nested collection. @param facts [Array<Facter::ResolvedFact>]

@return [FactCollection]

@api private

# File lib/facter/models/fact_collection.rb, line 20
def build_fact_collection!(facts)
  facts.each do |fact|
    next if %i[core legacy].include?(fact.type) && fact.value.nil?

    bury_fact(fact)
  end

  self
end
bury(*args) click to toggle source
# File lib/facter/models/fact_collection.rb, line 56
def bury(*args)
  raise ArgumentError, '2 or more arguments required' if args.count < 2

  if args.count == 2
    self[args[0]] = args[1]
  else
    arg = args.shift
    self[arg] = FactCollection.new unless self[arg]
    self[arg].bury(*args) unless args.empty?
  end

  self
end
dig_fact(user_query) click to toggle source
# File lib/facter/models/fact_collection.rb, line 30
def dig_fact(user_query)
  value(user_query)
rescue KeyError, TypeError
  nil
end
to_yaml() click to toggle source
# File lib/facter/models/fact_collection.rb, line 10
def to_yaml
  deep_to_h.to_yaml
end
value(user_query) click to toggle source

Collection#fetch implementation for nested collections. @param user_query [String] the search terms, separated by “.”

@return [String]

@example for fact_collection = { “os”: { “name”: “Darwin” } }

fact_collection.fetch("os.name") #=> "Darwin"

@api private

# File lib/facter/models/fact_collection.rb, line 45
def value(user_query)
  fetch(user_query) do
    split_user_query = Facter::Utils.split_user_query(user_query)
    split_user_query.reduce(self) do |memo, key|
      raise KeyError unless memo.respond_to?(:fetch)

      memo.fetch(key) { memo.fetch(key.to_s) }
    end
  end
end

Private Instance Methods

bury_fact(fact) click to toggle source
# File lib/facter/models/fact_collection.rb, line 78
def bury_fact(fact)
  split_fact_name = extract_fact_name(fact)
  bury(*split_fact_name << fact.value)
rescue NoMethodError
  @log.error("#{fact.type.to_s.capitalize} fact `#{fact.name}` cannot be added to collection."\
      ' The format of this fact is incompatible with other'\
      " facts that belong to `#{fact.name.split('.').first}` group")
end
deep_to_h(collection = self) click to toggle source
# File lib/facter/models/fact_collection.rb, line 72
def deep_to_h(collection = self)
  collection.each_pair.with_object({}) do |(key, value), hash|
    hash[key] = value.is_a?(FactCollection) ? deep_to_h(value) : value
  end
end
extract_fact_name(fact) click to toggle source
# File lib/facter/models/fact_collection.rb, line 87
def extract_fact_name(fact)
  case fact.type
  when :legacy
    [fact.name]
  when :custom, :external
    Options[:force_dot_resolution] == true ? fact.name.split('.') : [fact.name]
  else
    fact.name.split('.')
  end
end