Class: Facter::Util::Collection Private
- Inherits:
-
Object
- Object
- Facter::Util::Collection
- Includes:
- Enumerable
- Defined in:
- lib/facter/util/collection.rb
Overview
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.
Manage which facts exist and how we access them. Largely just a wrapper around a hash of facts.
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.
-
#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_loader ⇒ Object private
-
#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.
- #internal_loader ⇒ 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.
-
#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.
11 12 13 14 15 |
# File 'lib/facter/util/collection.rb', line 11 def initialize(internal_loader, external_loader) @facts = Hash.new @internal_loader = internal_loader @external_loader = external_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/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.
47 48 49 50 51 52 53 |
# File 'lib/facter/util/collection.rb', line 47 def add(name, = {}, &block) fact = create_or_return_fact(name, ) fact.add(, &block) return fact 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 37 38 |
# File 'lib/facter/util/collection.rb', line 28 def define_fact(name, = {}, &block) fact = create_or_return_fact(name, ) if block_given? fact.instance_eval(&block) end fact rescue => e Facter.log_exception(e, "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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/facter/util/collection.rb', line 58 def each load_all @facts.each do |name, fact| value = fact.value unless value.nil? yield name.to_s, value end end end |
#external_loader ⇒ 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.
112 113 114 |
# File 'lib/facter/util/collection.rb', line 112 def external_loader @external_loader 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/facter/util/collection.rb', line 69 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? Facter.warnonce("No facts loaded from #{internal_loader.search_path.join(File::PATH_SEPARATOR)}") end @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.
86 87 88 89 |
# File 'lib/facter/util/collection.rb', line 86 def flush @facts.each { |name, fact| fact.flush } @external_facts_loaded = nil end |
#internal_loader ⇒ 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.
108 109 110 |
# File 'lib/facter/util/collection.rb', line 108 def internal_loader @internal_loader 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.
92 93 94 95 |
# File 'lib/facter/util/collection.rb', line 92 def list load_all return @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.
97 98 99 100 |
# File 'lib/facter/util/collection.rb', line 97 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.
103 104 105 106 |
# File 'lib/facter/util/collection.rb', line 103 def load_all internal_loader.load_all load_external_facts 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.
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/facter/util/collection.rb', line 117 def to_hash @facts.inject({}) do |h, ary| value = ary[1].value if ! value.nil? # For backwards compatibility, convert the fact name to a string. h[ary[0].to_s] = value end h end end |