16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/lapiz.rb', line 16
def flatten
new_hash = self.dup
while new_hash.values.map(&:class).include?(Hash)
updated_hash = {}
new_hash.each_pair do |k,v|
if v.is_a?(Hash)
v.each_pair do |ik, iv|
updated_hash["#{k},#{ik}"] = iv
end
else
updated_hash[k.to_s] = v
end
end
new_hash = updated_hash
end
new_hash = new_hash.to_a.map{ |e|
if e[0].to_s.include?(",")
main = e[0].split(",").first
rest = e[0].split(",")[1..-1]
rest_string = rest.map{ |r| "[#{r}]" }.join("")
["#{main}#{rest_string}", e[1]]
else
[e[0].to_s, e[1]]
end
}.to_h
new_hash
end
|