Class: LegacyFacter::Util::Collection
- Inherits:
-
Object
- Object
- LegacyFacter::Util::Collection
- Includes:
- Enumerable
- Defined in:
- lib/custom_facts/util/collection.rb
Overview
Instance Attribute Summary collapse
- #external_loader ⇒ Object readonly
- #internal_loader ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ Object
Return a fact object by name.
-
#add(name, options = {}, &block) ⇒ LegacyFacter::Util::Fact
Add a resolution mechanism for a named fact.
-
#custom_facts ⇒ Object
Builds a hash of custom facts.
-
#define_fact(name, options = {}, &block) ⇒ LegacyFacter::Util::Fact
Define a new fact or extend an existing fact.
-
#each ⇒ Object
Iterate across all of the facts.
-
#external_facts ⇒ Object
Build a hash of external facts.
-
#fact(name) ⇒ Object
Return a fact by name.
-
#flush ⇒ Object
Flush all cached values.
-
#initialize(internal_loader, external_loader) ⇒ Collection
constructor
A new instance of Collection.
- #invalidate_custom_facts ⇒ Object
-
#list ⇒ Object
Return a list of all of the facts.
- #load(name) ⇒ Object
-
#load_all ⇒ Object
Load all known facts.
- #reload_custom_facts ⇒ Object
-
#to_hash ⇒ Object
Return a hash of all of our facts.
- #value(name) ⇒ Object
Constructor Details
#initialize(internal_loader, external_loader) ⇒ Collection
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_loader ⇒ Object (readonly)
136 137 138 |
# File 'lib/custom_facts/util/collection.rb', line 136 def external_loader @external_loader end |
#internal_loader ⇒ Object (readonly)
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.
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.
45 46 47 48 49 50 51 |
# File 'lib/custom_facts/util/collection.rb', line 45 def add(name, = {}, &block) fact = create_or_return_fact(name, ) fact.add(, &block) fact end |
#custom_facts ⇒ Object
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.[: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.
28 29 30 31 32 33 34 35 36 |
# File 'lib/custom_facts/util/collection.rb', line 28 def define_fact(name, = {}, &block) fact = create_or_return_fact(name, ) fact.instance_eval(&block) if block_given? fact rescue StandardError => e LegacyFacter.log_exception(e, "Unable to add fact #{name}: #{e}") end |
#each ⇒ Object
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_facts ⇒ Object
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.[:fact_type] } @external_facts = Facter::Utils.deep_copy(external_facts.keys) end |
#fact(name) ⇒ Object
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 |
#flush ⇒ Object
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_facts ⇒ Object
102 103 104 |
# File 'lib/custom_facts/util/collection.rb', line 102 def invalidate_custom_facts @valid_custom_facts = false end |
#list ⇒ Object
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
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_all ⇒ Object
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_facts ⇒ Object
106 107 108 |
# File 'lib/custom_facts/util/collection.rb', line 106 def reload_custom_facts @loaded = false end |
#to_hash ⇒ Object
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
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 |