Class: ForemanChef::FactImporter::Sparser

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_chef/fact_importer.rb

Instance Method Summary collapse

Instance Method Details

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



111
112
113
114
115
116
117
# File 'app/models/foreman_chef/fact_importer.rb', line 111

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) || Hash.new
end

#unsparse(hash, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'app/models/foreman_chef/fact_importer.rb', line 119

def unsparse(hash, options={})
  ret = Hash.new
  sparse(hash).each do |k, v|
    current            = ret
    key                = k.to_s.split(options.fetch(:separator, FactName::SEPARATOR))
    current            = (current[key.shift] ||= Hash.new) until (key.size<=1)
    current[key.first] = v
  end
  return ret
end