lib/cliutils/ext/hash_extensions.rb in cliutils-2.1.4 vs lib/cliutils/ext/hash_extensions.rb in cliutils-2.2.0
- old
+ new
@@ -1,25 +1,29 @@
# Hash Class
# Contains many convenient methods borrowed from Rails
# http://api.rubyonrails.org/classes/Hash.html
class Hash
- # Deep merges a hash into the current one.
+ # Deep merges a hash into the current one. Returns
+ # a new copy of the hash.
# @param [Hash] other_hash The hash to merge in
- # @yield &block
# @return [Hash] The original Hash
- def deep_merge!(other_hash, &block)
- other_hash.each_pair do |k, v|
- tv = self[k]
- if tv.is_a?(Hash) && v.is_a?(Hash)
- self[k] = tv.deep_merge(v, &block)
- else
- self[k] = block && tv ? block.call(k, tv, v) : v
- end
+ 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)
+ oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval
end
- self
end
+ # Deep merges a hash into the current one. Does
+ # the replacement inline.
+ # @param [Hash] other_hash The hash to merge in
+ # @return [Hash] The original Hash
+ def deep_merge!(other_hash)
+ replace(deep_merge(other_hash))
+ end
+
# Recursively turns all Hash keys into strings and
# returns the new Hash.
# @return [Hash] A new copy of the original Hash
def deep_stringify_keys
deep_transform_keys { |key| key.to_s }
@@ -64,10 +68,11 @@
# Recursively searches a hash for the passed
# key and returns the value (if there is one).
# http://stackoverflow.com/a/2239847/327179
# @param [<Symbol, String>] key The key to search for
+ # @yield
# @return [Multiple]
def recursive_find_by_key(key)
# Create a stack of hashes to search through for the needle which
# is initially this hash
stack = [ self ]
@@ -84,10 +89,9 @@
# ...push that on to the list of places to search.
stack << v
end
end
end
- yield if block_given?
end
private
# Modification to deep_transform_keys that allows for