Class: LegacyFacter::Util::Collection Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/facter/custom_facts/util/collection.rb

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(internal_loader, external_loader) ⇒ Collection

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.

Returns a new instance of Collection.



10
11
12
13
14
15
# File 'lib/facter/custom_facts/util/collection.rb', line 10

def initialize(internal_loader, external_loader)
  @facts = {}
  @internal_loader = internal_loader
  @external_loader = external_loader
  @loaded = false
end

Instance Attribute Details

#external_loaderObject (readonly)

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.



134
135
136
# File 'lib/facter/custom_facts/util/collection.rb', line 134

def external_loader
  @external_loader
end

#internal_loaderObject (readonly)

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.



132
133
134
# File 'lib/facter/custom_facts/util/collection.rb', line 132

def internal_loader
  @internal_loader
end

Instance Method Details

#[](name) ⇒ Object

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.

Return a fact object by name.



18
19
20
# File 'lib/facter/custom_facts/util/collection.rb', line 18

def [](name)
  value(name)
end

#add(name, options = {}, &block) ⇒ Facter::Util::Fact

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.

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.

Parameters:

  • name (Symbol)

    The name of the fact to define

  • options (Hash) (defaults to: {})

    A hash of options to set on the fact and resolution

Returns:



45
46
47
48
49
50
51
# File 'lib/facter/custom_facts/util/collection.rb', line 45

def add(name, options = {}, &block)
  fact = create_or_return_fact(name, options)

  fact.add(options, &block)

  fact
end

#custom_factsObject

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.

Builds a hash of custom facts



110
111
112
113
114
115
116
117
118
119
# File 'lib/facter/custom_facts/util/collection.rb', line 110

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 }
end

#define_fact(name, options = {}, &block) ⇒ Facter::Util::Fact

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.

Define a new fact or extend an existing fact.

Parameters:

  • name (Symbol)

    The name of the fact to define

  • options (Hash) (defaults to: {})

    A hash of options to set on the fact

Returns:



28
29
30
31
32
33
34
35
36
# File 'lib/facter/custom_facts/util/collection.rb', line 28

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

#eachObject

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.

Iterate across all of the facts.



56
57
58
59
60
61
62
# File 'lib/facter/custom_facts/util/collection.rb', line 56

def each
  load_all
  @facts.each do |name, fact|
    value = fact.value
    yield name.to_s, value unless value.nil?
  end
end

#external_factsObject

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.

Build a hash of external facts



94
95
96
97
98
99
# File 'lib/facter/custom_facts/util/collection.rb', line 94

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) ⇒ Object

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.

Return a fact by name.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/facter/custom_facts/util/collection.rb', line 65

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

#flushObject

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.

Flush all cached values.



82
83
84
85
# File 'lib/facter/custom_facts/util/collection.rb', line 82

def flush
  @facts.each { |_name, fact| fact.flush }
  @external_facts_loaded = nil
end

#invalidate_custom_factsObject

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.



101
102
103
# File 'lib/facter/custom_facts/util/collection.rb', line 101

def invalidate_custom_facts
  @valid_custom_facts = false
end

#listObject

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.

Return a list of all of the facts.



88
89
90
91
# File 'lib/facter/custom_facts/util/collection.rb', line 88

def list
  load_all
  @facts.keys
end

#load(name) ⇒ Object

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.



121
122
123
124
# File 'lib/facter/custom_facts/util/collection.rb', line 121

def load(name)
  internal_loader.load(name)
  load_external_facts
end

#load_allObject

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.

Load all known facts.



127
128
129
130
# File 'lib/facter/custom_facts/util/collection.rb', line 127

def load_all
  internal_loader.load_all
  load_external_facts
end

#reload_custom_factsObject

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.



105
106
107
# File 'lib/facter/custom_facts/util/collection.rb', line 105

def reload_custom_facts
  @loaded = false
end

#to_hashObject

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.

Return a hash of all of our facts.



137
138
139
140
141
142
143
144
145
# 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) ⇒ Object

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.



147
148
149
150
# File 'lib/facter/custom_facts/util/collection.rb', line 147

def value(name)
  fact = fact(name)
  fact&.value
end