Class: ForemanAnsible::FactSparser

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_ansible/fact_sparser.rb

Overview

See sparse and unsparse documentation

Class Method Summary collapse

Class Method Details

.sparse(hash, options = {}) ⇒ Object

Sparses facts, so that it converts a facts hash { operatingsystem : { major: 20, name : ‘fedora’ } into { operatingsystem::major: 20,

operatingsystem::name: 'fedora' }


12
13
14
15
16
17
18
# File 'app/services/foreman_ansible/fact_sparser.rb', line 12

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

.unsparse(facts_hash) ⇒ Object

Unsparses facts, so that it converts a hash with facts { operatingsystem::major: 20,

operatingsystem::name: 'fedora' }

into { operatingsystem : { major: 20, name: ‘fedora’ } }



25
26
27
28
29
30
31
32
33
34
# File 'app/services/foreman_ansible/fact_sparser.rb', line 25

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