Created
May 12, 2010 16:42
-
-
Save tcocca/398805 to your computer and use it in GitHub Desktop.
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/class/attribute_accessors' | |
| require 'active_support/core_ext/array' | |
| require 'active_support/inflector' | |
| require 'active_support/core_ext/class/inheritable_attributes' | |
| require 'active_support/core_ext/duplicable' | |
| class Hash #:nodoc: | |
| def deep_merge(other_hash) | |
| self.merge(other_hash) do |key, oldval, newval| | |
| oldval = oldval.to_hash if oldval.respond_to?(:to_hash) | |
| newval = newval.to_hash if newval.respond_to?(:to_hash) | |
| if oldval.is_a?(Hash) && newval.is_a?(Hash) | |
| oldval.deep_merge(newval) | |
| elsif oldval.is_a?(Array) && newval.is_a?(Array) | |
| oldval += newval | |
| else | |
| newval | |
| end | |
| end | |
| end | |
| def deep_merge!(other_hash) | |
| replace(deep_merge(other_hash)) | |
| end | |
| def deep_symbolize_keys | |
| self.inject({}) { |result, (key, value)| | |
| value = value.deep_symbolize_keys if value.is_a?(Hash) | |
| result[(key.to_sym rescue key) || key] = value | |
| result | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment