Created
November 20, 2010 14:24
-
-
Save lukaszx0/707858 to your computer and use it in GitHub Desktop.
Flattening hashes with namespaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/core_ext/hash/deep_merge" | |
class Hash | |
@@namespace = [] | |
def namespace_flatten(separator = ".") | |
hash = {} | |
self.each do |key, value| | |
key = key.to_s | |
if value.is_a?(Hash) | |
@@namespace << key | |
hash.merge! value.namespace_flatten(separator) | |
@@namespace.pop | |
else | |
key = @@namespace + [key] | |
hash[key.join(separator)] = value | |
end | |
end | |
hash | |
end | |
def namespace_unflatten(separator = ".") | |
hash = {} | |
self.each do |key, value| | |
hash.deep_merge! self.class.nested_value(value, key.to_s.split(separator)) | |
end | |
hash | |
end | |
def self.nested_value(x, p) | |
hash = { p.pop => x } | |
p.reverse_each do |element| | |
hash = { element => hash } | |
end | |
hash | |
end | |
#TODO - refactor this, looks ugly. Avoid duplications | |
def recursive_symbolize_keys! | |
symbolize_keys! | |
values.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) } | |
values.select { |v| v.is_a?(Array) }.flatten.each { |h| h.recursive_symbolize_keys! if h.is_a?(Hash) } | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment