Module: Metanorma::Utils::Hash

Defined in:
lib/utils/hash_transform_keys.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(second) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/utils/hash_transform_keys.rb', line 58

def deep_merge(second)
  merger = proc { |_, v1, v2|
    if ::Hash === v1 && ::Hash === v2
      v1.merge(v2, &merger)
    elsif ::Array === v1 && ::Array === v2
      v1 | v2
    elsif [:undefined].include?(v2)
      v1
    else
      v2
    end
  }
  merge(second.to_h, &merger)
end

#stringify_all_keysObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/utils/hash_transform_keys.rb', line 32

def stringify_all_keys
  result = {}
  each do |k, v|
    result[k.to_s] = case v
                     when ::Hash, ::Array
                       v.stringify_all_keys
                     else
                       v
                     end
  end
  result
end

#symbolize_all_keysObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utils/hash_transform_keys.rb', line 45

def symbolize_all_keys
  result = {}
  each do |k, v|
    result[k.to_sym] = case v
                       when ::Hash, ::Array
                         v.symbolize_all_keys
                       else
                         v
                       end
  end
  result
end