Class: Facter::FactCollection

Inherits:
Hash
  • Object
show all
Defined in:
lib/facter/models/fact_collection.rb

Instance Method Summary collapse

Constructor Details

#initializeFactCollection

Returns a new instance of FactCollection.



5
6
7
8
# File 'lib/facter/models/fact_collection.rb', line 5

def initialize
  super
  @log = Log.new(self)
end

Instance Method Details

#build_fact_collection!(facts) ⇒ FactCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transorms a list of ResolvedFact into a nested collection.

Parameters:

Returns:



20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



30
31
32
33
34
# File 'lib/facter/models/fact_collection.rb', line 30

def dig_fact(user_query)
  value(user_query)
rescue KeyError, TypeError
  nil
end

#to_yamlObject



10
11
12
# File 'lib/facter/models/fact_collection.rb', line 10

def to_yaml
  deep_to_h.to_yaml
end

#value(user_query) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Collection#fetch implementation for nested collections.

Examples:

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

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

Parameters:

  • user_query (String)

    the search terms, separated by “.”

Returns:

  • (String)


45
46
47
48
49
50
51
52
53
54
# 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