Class: RFacter::Util::Collection Private
- Inherits:
-
Object
- Object
- RFacter::Util::Collection
- Extended by:
- Forwardable
- Includes:
- Concurrent::Async, Enumerable, DSL
- Defined in:
- lib/rfacter/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 on a Node and how we access them.
Largely just a wrapper around a hash of facts that have been retrieved from a particular node.
Constant Summary
Constants included from DSL
Instance Method Summary collapse
-
#[](name) ⇒ Object
private
Return a fact object by name.
-
#add(name, options = {}, &block) ⇒ RFacter::Util::Fact
private
Add a resolution mechanism for a named fact.
-
#define_fact(name, options = {}, &block) ⇒ RFacter::Util::Fact
private
Define a new fact or extend an existing fact.
-
#each ⇒ Object
private
Iterate across all of the facts.
-
#fact(name) ⇒ Object
private
Return a fact by name.
-
#flush ⇒ Object
private
Flush all cached values.
-
#initialize(node, config: RFacter::Config.config, **opts) ⇒ Collection
constructor
private
Initialize a new Collection object.
-
#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(node, config: RFacter::Config.config, **opts) ⇒ 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.
Initialize a new Collection object
31 32 33 34 35 36 37 |
# File 'lib/rfacter/util/collection.rb', line 31 def initialize(node, config: RFacter::Config.config, **opts) @node = node @config = config @facts = Hash.new @internal_loader = RFacter::Util::Loader.new 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.
40 41 42 |
# File 'lib/rfacter/util/collection.rb', line 40 def [](name) value(name) end |
#add(name, options = {}, &block) ⇒ RFacter::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.
69 70 71 72 73 74 75 |
# File 'lib/rfacter/util/collection.rb', line 69 def add(name, = {}, &block) fact = create_or_return_fact(name, ) fact.add(, &block) return fact end |
#define_fact(name, options = {}, &block) ⇒ RFacter::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.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rfacter/util/collection.rb', line 50 def define_fact(name, = {}, &block) fact = create_or_return_fact(name, ) if block_given? fact.instance_eval(&block) end fact rescue => e logger.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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rfacter/util/collection.rb', line 80 def each load_all COLLECTION.bind(self) do NODE.bind(@node) do @facts.each do |name, fact| value = fact.value unless value.nil? yield name.to_s, value end end end end 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rfacter/util/collection.rb', line 96 def fact(name) name = canonicalize(name) # Try to load the fact if necessary load(name) unless @facts[name] # Try HARDER load_all unless @facts[name] if @facts.empty? logger.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.
113 114 115 |
# File 'lib/rfacter/util/collection.rb', line 113 def flush @facts.each { |name, fact| fact.flush } 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.
118 119 120 121 |
# File 'lib/rfacter/util/collection.rb', line 118 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.
123 124 125 |
# File 'lib/rfacter/util/collection.rb', line 123 def load(name) @internal_loader.load(name, self) 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.
128 129 130 |
# File 'lib/rfacter/util/collection.rb', line 128 def load_all @internal_loader.load_all(self) 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.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rfacter/util/collection.rb', line 133 def to_hash COLLECTION.bind(self) do NODE.bind(@node) do @facts.each_with_object({}) do |(name, fact), hash| resolved_value = fact.value # For backwards compatibility, convert the fact name to a string. hash[name.to_s] = resolved_value unless resolved_value.nil? end 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.
146 147 148 149 150 151 152 153 154 |
# File 'lib/rfacter/util/collection.rb', line 146 def value(name) COLLECTION.bind(self) do NODE.bind(@node) do if fact = fact(name) fact.value end end end end |