Sha256: cd09abf88e08c897b20aa6a331f876f798428d199de98e6c07038b0dabb35b37

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Facter
  class FactCollection < Hash
    def initialize
      super
      @log = Log.new(self)
    end

    def to_yaml
      deep_to_h.to_yaml
    end

    def build_fact_collection!(facts)
      facts.each do |fact|
        next if %i[core legacy].include?(fact.type) && fact.value.nil?

        bury_fact(fact)
      end

      self
    end

    def dig_fact(user_query)
      value(user_query)
    rescue KeyError
      nil
    end

    def value(user_query)
      fetch(user_query) do
        split_user_query = Facter::Utils.split_user_query(user_query)
        split_user_query.reduce(self) do |memo, key|
          memo.fetch(key) { memo.fetch(key.to_s) } if memo.is_a?(Hash) || memo.is_a?(Array)
        end
      end
    end

    def bury(*args)
      raise ArgumentError, '2 or more arguments required' if args.count < 2

      if args.count == 2
        self[args[0]] = args[1]
      else
        arg = args.shift
        self[arg] = FactCollection.new unless self[arg]
        self[arg].bury(*args) unless args.empty?
      end

      self
    end

    private

    def deep_to_h(collection = self)
      collection.each_pair.with_object({}) do |(key, value), hash|
        hash[key] = value.is_a?(FactCollection) ? deep_to_h(value) : value
      end
    end

    def bury_fact(fact)
      split_fact_name = extract_fact_name(fact)
      bury(*split_fact_name + fact.filter_tokens << fact.value)
    rescue NoMethodError
      @log.error("#{fact.type.to_s.capitalize} fact `#{fact.name}` cannot be added to collection."\
          ' The format of this fact is incompatible with other'\
          " facts that belong to `#{fact.name.split('.').first}` group")
    end

    def extract_fact_name(fact)
      case fact.type
      when :legacy
        [fact.name]
      when :custom, :external
        Options[:force_dot_resolution] == true ? fact.name.split('.') : [fact.name]
      else
        fact.name.split('.')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facter-4.1.1 lib/facter/models/fact_collection.rb