lib/cliutils/ext/hash_extensions.rb in cliutils-2.2.1 vs lib/cliutils/ext/hash_extensions.rb in cliutils-2.2.2

- old
+ new

@@ -8,11 +8,12 @@ # @return [Hash] The original Hash 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 + oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? + oldval.deep_merge(newval) : newval end end # Deep merges a hash into the current one. Does # the replacement inline. @@ -62,9 +63,24 @@ # alters the original Hash. # @yield &block # @return [Hash] The original Hash def deep_transform_keys!(&block) _deep_transform_keys_in_object!(self, &block) + end + + # Allows for dot-notation getting/setting within + # a Hash. + # http://ceronio.net/2012/01/javascript-style-object-value-assignment-in-ruby/ + # @param [<String, Symbol>] meth The method name + # @param [Array] args Any passed arguments + # @yield if a block is passed + def method_missing(meth, *args, &block) + meth_name = meth.to_s + if meth_name[-1,1] == '=' + self[meth_name[0..-2].to_sym] = args[0] + else + self[meth] || self[meth_name] + end end # Recursively searches a hash for the passed # key and returns the value (if there is one). # http://stackoverflow.com/a/2239847/327179