class LegacyFacter::Util::Collection
Attributes
Public Class Methods
# 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
Public Instance Methods
Return a fact object by name.
# File lib/facter/custom_facts/util/collection.rb, line 18 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 [Facter::Util::Fact] The fact that was defined
# File lib/facter/custom_facts/util/collection.rb, line 45 def add(name, options = {}, &block) fact = create_or_return_fact(name, options) fact.add(options, &block) fact end
# File lib/facter/custom_facts/util/collection.rb, line 109 def custom_fact(fact_name) internal_loader.load(fact_name) @custom_facts = @facts.select { |_k, v| v.options[:fact_type] == :custom } end
Builds a hash of custom facts
# File lib/facter/custom_facts/util/collection.rb, line 115 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.options[:fact_type] == :custom } 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 [Facter::Util::Fact] The fact that was defined
# File lib/facter/custom_facts/util/collection.rb, line 28 def define_fact(name, options = {}, &block) fact = create_or_return_fact(name, options) fact.instance_eval(&block) if block_given? fact rescue StandardError => e Facter.log_exception(e, "Unable to add fact #{name}: #{e}") end
Iterate across all of the facts.
# 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
Build a hash of external facts
# File lib/facter/custom_facts/util/collection.rb, line 94 def external_facts return @external_facts unless @external_facts.nil? load_external_facts @external_facts = @facts.select { |_k, v| v.options[:fact_type] == :external } end
Return a fact by name.
# 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] if @facts.empty? LegacyFacter.warnonce("No facts loaded from #{internal_loader.search_path.join(File::PATH_SEPARATOR)}") end @facts[name] end
Flush all cached values.
# File lib/facter/custom_facts/util/collection.rb, line 82 def flush @facts.each { |_name, fact| fact.flush } @external_facts_loaded = nil end
# File lib/facter/custom_facts/util/collection.rb, line 101 def invalidate_custom_facts @valid_custom_facts = false end
Return a list of all of the facts.
# File lib/facter/custom_facts/util/collection.rb, line 88 def list load_all @facts.keys end
# File lib/facter/custom_facts/util/collection.rb, line 126 def load(name) internal_loader.load(name) load_external_facts end
Load all known facts.
# File lib/facter/custom_facts/util/collection.rb, line 132 def load_all internal_loader.load_all load_external_facts end
# File lib/facter/custom_facts/util/collection.rb, line 105 def reload_custom_facts @loaded = false end
Return a hash of all of our facts.
# File lib/facter/custom_facts/util/collection.rb, line 142 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
# File lib/facter/custom_facts/util/collection.rb, line 152 def value(name) fact = fact(name) fact&.value end
Private Instance Methods
# File lib/facter/custom_facts/util/collection.rb, line 174 def canonicalize(name) name.to_s.downcase.to_sym end
# File lib/facter/custom_facts/util/collection.rb, line 159 def create_or_return_fact(name, options) name = canonicalize(name) fact = @facts[name] if fact.nil? fact = Facter::Util::Fact.new(name, options) @facts[name] = fact else fact.extract_ldapname_option!(options) end fact end
# File lib/facter/custom_facts/util/collection.rb, line 178 def load_external_facts return if @external_facts_loaded @external_facts_loaded = true external_loader.load(self) end