Sha256: 91c898ce2fe79f425d42f359188f6918555a319d56b8be97e09ef9813ce76eac

Contents?: true

Size: 1.21 KB

Versions: 46

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module ForemanAnsible
  # See sparse and unsparse documentation
  class FactSparser
    class << self
      # Sparses facts, so that it converts a facts hash
      # { operatingsystem : { major: 20, name : 'fedora' }
      # into
      # { operatingsystem::major: 20,
      #   operatingsystem::name: 'fedora' }
      def sparse(hash, options = {})
        hash.map do |k, v|
          prefix = options.fetch(:prefix, []) + [k]
          next sparse(v, options.merge(:prefix => prefix)) if v.is_a? Hash
          { prefix.join(options.fetch(:separator, FactName::SEPARATOR)) => v }
        end.reduce(:merge) || {}
      end

      # Unsparses facts, so that it converts a hash with facts
      # { operatingsystem::major: 20,
      #   operatingsystem::name: 'fedora' }
      # into
      # { operatingsystem : { major: 20, name: 'fedora' } }
      def unsparse(facts_hash)
        ret = {}
        sparse(facts_hash).each do |full_name, value|
          current = ret
          fact_name = full_name.to_s.split(FactName::SEPARATOR)
          current = (current[fact_name.shift] ||= {}) until fact_name.size <= 1
          current[fact_name.first] = value
        end
        ret
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
foreman_ansible-6.3.4 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.3.3 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.3.2 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.3.1 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.4.1 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.4.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.3.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.2.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.1.1 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.0.2 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.1.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.0.1 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-5.1.3 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-6.0.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-5.1.2 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-5.1.1 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-5.1.0 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-4.0.3.5 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-4.0.3.4 app/services/foreman_ansible/fact_sparser.rb
foreman_ansible-4.0.3.3 app/services/foreman_ansible/fact_sparser.rb