Sha256: 3e0470be7d6ce3b830a48b654e8b69d627da23a5e303dafb22447e79deb84c41

Contents?: true

Size: 865 Bytes

Versions: 29

Compression:

Stored size: 865 Bytes

Contents

# frozen_string_literal: true

module Facter
  class FactCollection < Hash
    def initialize
      super
    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)
      bury(*fact.name.split('.') + fact.filter_tokens << fact.value)
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
facter-4.0.24 lib/models/fact_collection.rb
facter-4.0.23 lib/models/fact_collection.rb
facter-4.0.22 lib/models/fact_collection.rb
facter-4.0.21 lib/models/fact_collection.rb
facter-4.0.20 lib/models/fact_collection.rb
facter-4.0.19 lib/models/fact_collection.rb
facter-4.0.18 lib/models/fact_collection.rb
facter-4.0.17 lib/models/fact_collection.rb
facter-4.0.16 lib/models/fact_collection.rb