Sha256: 9802738f299a24759473bef715d469e1373730c14d3062e43fea9bf29038dee8
Contents?: true
Size: 1.36 KB
Versions: 10
Compression:
Stored size: 1.36 KB
Contents
# load 'the_role/hash.rb' - UPDATE, BUT NOT RELOAD [for console testing] class Hash # deep_transform_keys # deep_stringify_keys # underscorify_keys # deep_reset unless {}.respond_to?(:deep_transform_keys) def deep_transform_keys(&block) result = {} each do |key, value| result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value end result end def deep_transform_keys!(&block) keys.each do |key| value = delete(key) self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value end self end end unless {}.respond_to?(:deep_stringify_keys) def deep_stringify_keys deep_transform_keys{ |key| key.to_s } end def deep_stringify_keys! deep_transform_keys!{ |key| key.to_s } end end unless {}.respond_to?(:underscorify_keys) def underscorify_keys deep_transform_keys{ |key| key.to_slug_param(sep: '_') } end def underscorify_keys! replace underscorify_keys end end unless {}.respond_to?(:deep_reset) def deep_reset(default = nil) hash = dup hash.each do |key, value| hash[key] = hash[key].is_a?(Hash) ? hash[key].deep_reset(default) : default end hash end def deep_reset!(default = nil) replace deep_reset(default) end end end
Version data entries
10 entries across 10 versions & 1 rubygems