Class: LegacyFacter::Util::Collection

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

Overview

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(internal_loader, external_loader) ⇒ Collection

Returns a new instance of Collection.

Since:

  • 2.0.0



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)

Since:

  • 2.0.0



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

def external_loader
  @external_loader
end

#internal_loaderObject (readonly)

Since:

  • 2.0.0



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

def internal_loader
  @internal_loader
end

Instance Method Details

#[](name) ⇒ Object

Return a fact object by name.

Since:

  • 2.0.0



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

def [](name)
  value(name)
end

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

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:

Since:

  • 2.0.0



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

Builds a hash of custom facts

Since:

  • 2.0.0



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) ⇒ LegacyFacter::Util::Fact

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:

Since:

  • 2.0.0



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
  LegacyFacter.log_exception(e, "Unable to add fact #{name}: #{e}")
end

#eachObject

Iterate across all of the facts.

Since:

  • 2.0.0



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

Build a hash of external facts

Since:

  • 2.0.0



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

Return a fact by name.

Since:

  • 2.0.0



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

Flush all cached values.

Since:

  • 2.0.0



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

Since:

  • 2.0.0



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

def invalidate_custom_facts
  @valid_custom_facts = false
end

#listObject

Return a list of all of the facts.

Since:

  • 2.0.0



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

def list
  load_all
  @facts.keys
end

#load(name) ⇒ Object

Since:

  • 2.0.0



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

Load all known facts.

Since:

  • 2.0.0



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

Since:

  • 2.0.0



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

def reload_custom_facts
  @loaded = false
end

#to_hashObject

Return a hash of all of our facts.

Since:

  • 2.0.0



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

Since:

  • 2.0.0



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