class RFacter::Util::Collection
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.
@api private @since 0.1.0
Public Class Methods
Initialize a new Collection
object
@param node [RFacter::Node] The node from which this collection
should retrieve facts.
# 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
Public Instance Methods
Return a fact object by name.
# File lib/rfacter/util/collection.rb, line 40 def [](name) value(name) end
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.
@param name [Symbol] The name of the fact to define @param options [Hash] A hash of options to set on the fact and resolution
@return [RFacter::Util::Fact] The fact that was defined
# File lib/rfacter/util/collection.rb, line 69 def add(name, options = {}, &block) fact = create_or_return_fact(name, options) fact.add(options, &block) return fact end
Define a new fact or extend an existing fact.
@param name [Symbol] The name of the fact to define @param options [Hash] A hash of options to set on the fact
@return [RFacter::Util::Fact] The fact that was defined
# File lib/rfacter/util/collection.rb, line 50 def define_fact(name, options = {}, &block) fact = create_or_return_fact(name, options) if block_given? fact.instance_eval(&block) end fact rescue => e logger.log_exception(e, "Unable to add fact #{name}: #{e}") end
Iterate across all of the facts.
# 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
Return a fact by name.
# 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 all cached values.
# File lib/rfacter/util/collection.rb, line 113 def flush @facts.each { |name, fact| fact.flush } end
Return a list of all of the facts.
# File lib/rfacter/util/collection.rb, line 118 def list load_all return @facts.keys end
# File lib/rfacter/util/collection.rb, line 123 def load(name) @internal_loader.load(name, self) end
Load all known facts.
# File lib/rfacter/util/collection.rb, line 128 def load_all @internal_loader.load_all(self) end
Return a hash of all of our facts.
# 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
# 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
Private Instance Methods
# File lib/rfacter/util/collection.rb, line 171 def canonicalize(name) name.to_s.downcase.to_sym end
# File lib/rfacter/util/collection.rb, line 158 def create_or_return_fact(name, options) name = canonicalize(name) fact = @facts[name] if fact.nil? fact = RFacter::Util::Fact.new(name, options) @facts[name] = fact end fact end