Sha256: 2873e22b1e9991ffdb3fa576012dcba688222b198c1ede06874d18968490599c

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Facter
  module FactAugmenter
    def self.augment_resolved_facts(searched_facts, resolved_facts)
      augumented_resolved_facts = []
      searched_facts.each do |searched_fact|
        matched_facts = get_resolved_facts_for_searched_fact(searched_fact, resolved_facts)
        augment_resolved_fact_for_user_query!(searched_fact, matched_facts)
        augumented_resolved_facts.concat(matched_facts)
      end

      augumented_resolved_facts
    end

    private_class_method def self.get_resolved_facts_for_searched_fact(searched_fact, resolved_facts)
      if searched_fact.name.include?('.*')
        resolved_facts
          .select { |resolved_fact| resolved_fact.name.match(searched_fact.name) }
          .reject(&:user_query)
          .uniq(&:name)
      else
        resolved_facts.select do |resolved_fact|
          valid_fact(searched_fact, resolved_fact)
        end.reject(&:user_query).uniq(&:name)
      end
    end

    private_class_method def self.augment_resolved_fact_for_user_query!(searched_fact, matched_facts)
      matched_facts.each do |matched_fact|
        matched_fact.user_query = searched_fact.user_query
        matched_fact.filter_tokens = searched_fact.filter_tokens
      end
    end

    private_class_method def self.valid_fact(searched_fact, resolved_fact)
      return false unless searched_fact.name.eql?(resolved_fact.name)

      if searched_fact.filter_tokens.any?
        case resolved_fact.value
        when Array, Hash
          begin
            resolved_fact.value.dig(*searched_fact.filter_tokens)
          rescue TypeError
            return false
          end
        else
          return false
        end
      end
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facter-4.1.1 lib/facter/framework/core/fact_augmenter.rb
facter-4.1.0 lib/facter/framework/core/fact_augmenter.rb