Class: LegacyFacter::Util::Collection Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/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/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.



136
137
138
# File 'lib/custom_facts/util/collection.rb', line 136

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.



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

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/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/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



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

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) ⇒ 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/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/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
100
# File 'lib/custom_facts/util/collection.rb', line 94

def external_facts
  return @external_facts unless @external_facts.nil?

  load_external_facts
  external_facts = @facts.reject { |_k, v| v.options[:fact_type] }
  @external_facts = Facter::Utils.deep_copy(external_facts.keys)
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/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/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.



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

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/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.



123
124
125
126
# File 'lib/custom_facts/util/collection.rb', line 123

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.



129
130
131
132
# File 'lib/custom_facts/util/collection.rb', line 129

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.



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

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.



139
140
141
142
143
144
145
146
147
# File 'lib/custom_facts/util/collection.rb', line 139

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.



149
150
151
152
153
154
155
156
# File 'lib/custom_facts/util/collection.rb', line 149

def value(name)
  fact = fact(name)
  fact_value = fact&.value
  return Facter.core_value(name) if fact_value.nil?

  core_value = Facter.core_value(name) if fact.used_resolution_weight <= 0
  core_value.nil? ? fact_value : core_value
end