Class: LegacyFacter::Util::Collection Private
- Inherits:
-
Object
- Object
- LegacyFacter::Util::Collection
- 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
- #external_loader ⇒ Object readonly private
- #internal_loader ⇒ Object readonly private
Instance Method Summary collapse
-
#[](name) ⇒ Object
private
Return a fact object by name.
-
#add(name, options = {}, &block) ⇒ Facter::Util::Fact
private
Add a resolution mechanism for a named fact.
- #custom_fact(fact_name) ⇒ Object private
-
#custom_facts ⇒ Object
private
Builds a hash of custom facts.
-
#define_fact(name, options = {}, &block) ⇒ Facter::Util::Fact
private
Define a new fact or extend an existing fact.
-
#each ⇒ Object
private
Iterate across all of the facts.
-
#external_facts ⇒ Object
private
Build a hash of external facts.
-
#fact(name) ⇒ Object
private
Return a fact by name.
-
#flush ⇒ Object
private
Flush all cached values.
-
#initialize(internal_loader, external_loader) ⇒ Collection
constructor
private
A new instance of Collection.
- #invalidate_custom_facts ⇒ Object private
-
#list ⇒ Object
private
Return a list of all of the facts.
- #load(name) ⇒ Object private
-
#load_all ⇒ Object
private
Load all known facts.
- #reload_custom_facts ⇒ Object private
-
#to_hash ⇒ Object
private
Return a hash of all of our facts.
- #value(name) ⇒ Object private
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_loader ⇒ Object (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.
137 138 139 |
# File 'lib/facter/custom_facts/util/collection.rb', line 137 def external_loader @external_loader end |
#internal_loader ⇒ Object (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.
135 136 137 |
# File 'lib/facter/custom_facts/util/collection.rb', line 135 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.
45 46 47 48 49 50 51 |
# File 'lib/facter/custom_facts/util/collection.rb', line 45 def add(name, = {}, &block) fact = create_or_return_fact(name, ) fact.add(, &block) fact end |
#custom_fact(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.
107 108 109 110 |
# File 'lib/facter/custom_facts/util/collection.rb', line 107 def custom_fact(fact_name) internal_loader.load(fact_name) @custom_facts = @facts.select { |_k, v| v.[:fact_type] == :custom } end |
#custom_facts ⇒ 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.
Builds a hash of custom facts
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/facter/custom_facts/util/collection.rb', line 113 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 } 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.
28 29 30 31 32 33 34 35 36 |
# File 'lib/facter/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 log.log_exception("Unable to add fact #{name}: #{e}") end |
#each ⇒ 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.
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_facts ⇒ 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.
Build a hash of external facts
92 93 94 95 96 97 |
# File 'lib/facter/custom_facts/util/collection.rb', line 92 def external_facts return @external_facts unless @external_facts.nil? load_external_facts @external_facts = @facts.select { |_k, v| v.[: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 |
# 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] log.warnonce("No facts loaded from #{internal_loader.search_path.join(File::PATH_SEPARATOR)}") if @facts.empty? @facts[name] end |
#flush ⇒ 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.
Flush all cached values.
80 81 82 83 |
# File 'lib/facter/custom_facts/util/collection.rb', line 80 def flush @facts.each { |_name, fact| fact.flush } @external_facts_loaded = nil end |
#invalidate_custom_facts ⇒ 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.
99 100 101 |
# File 'lib/facter/custom_facts/util/collection.rb', line 99 def invalidate_custom_facts @valid_custom_facts = false end |
#list ⇒ 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 list of all of the facts.
86 87 88 89 |
# File 'lib/facter/custom_facts/util/collection.rb', line 86 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.
124 125 126 127 |
# File 'lib/facter/custom_facts/util/collection.rb', line 124 def load(name) internal_loader.load(name) load_external_facts end |
#load_all ⇒ 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.
Load all known facts.
130 131 132 133 |
# File 'lib/facter/custom_facts/util/collection.rb', line 130 def load_all internal_loader.load_all load_external_facts end |
#reload_custom_facts ⇒ 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.
103 104 105 |
# File 'lib/facter/custom_facts/util/collection.rb', line 103 def reload_custom_facts @loaded = false end |
#to_hash ⇒ 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 hash of all of our facts.
140 141 142 143 144 145 146 147 148 |
# File 'lib/facter/custom_facts/util/collection.rb', line 140 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.
150 151 152 153 |
# File 'lib/facter/custom_facts/util/collection.rb', line 150 def value(name) fact = fact(name) fact&.value end |