Class: ForemanChef::FactImporter::Sparser
- Inherits:
-
Object
- Object
- ForemanChef::FactImporter::Sparser
- Defined in:
- app/models/foreman_chef/fact_importer.rb
Instance Method Summary collapse
Instance Method Details
#sparse(hash, options = {}) ⇒ Object
107 108 109 110 111 112 113 |
# File 'app/models/foreman_chef/fact_importer.rb', line 107 def sparse(hash, ={}) hash.map do |k, v| prefix = (.fetch(:prefix, [])+[k]) next sparse(v, .merge(:prefix => prefix)) if v.is_a? Hash { prefix.join(.fetch(:separator, FactName::SEPARATOR)) => v } end.reduce(:merge) || Hash.new end |
#unsparse(hash, options = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/foreman_chef/fact_importer.rb', line 115 def unsparse(hash, ={}) ret = Hash.new sparse(hash).each do |k, v| current = ret key = k.to_s.split(.fetch(:separator, FactName::SEPARATOR)) current = (current[key.shift] ||= Hash.new) until (key.size<=1) current[key.first] = v end return ret end |