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
111 112 113 114 115 116 117 |
# File 'app/models/foreman_chef/fact_importer.rb', line 111 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
119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/foreman_chef/fact_importer.rb', line 119 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 |