Module: Droppper::Utils
Instance Method Summary collapse
Instance Method Details
#print_hash(hash, include_empty = false) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/droppper/utils.rb', line 3 def print_hash(hash, include_empty=false) lines = [] recursive_flatten_hash(hash).each do |key, value| lines << "#{key.upcase}: #{value}" if include_empty || value.present? end lines.join("\n") end |
#recursive_flatten_hash(hash) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/droppper/utils.rb', line 11 def recursive_flatten_hash(hash) h = {} hash.each do |key, value| if value.is_a?(Array) value = value.map.with_index{|v, i| {i => v} }.inject(&:merge) end if value.is_a?(Hash) recursive_flatten_hash(value).each do |key2, value2| h["#{key}.#{key2}"] = value2 end else h[key] = value end end h end |