Sha256: d6f5008a58a4280b7568273909f0ec6d9181ca60d579184f6be23c3f95363cd1

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Facter
  class FactCollection < Hash
    def initialize
      super
      @log = Log.new(self)
    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 value(*keys)
      keys.reduce(self) do |memo, key|
        memo.fetch(key.to_s)
      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 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)
      fact.type == :legacy ? [fact.name] : fact.name.split('.')
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facter-4.0.52 lib/facter/models/fact_collection.rb
facter-4.0.51 lib/facter/models/fact_collection.rb
facter-4.0.50 lib/facter/models/fact_collection.rb
facter-4.0.49 lib/facter/models/fact_collection.rb
facter-4.0.48 lib/facter/models/fact_collection.rb
facter-4.0.47 lib/facter/models/fact_collection.rb