lib/mpatch/hash.rb in mpatch-2.11.0 vs lib/mpatch/hash.rb in mpatch-2.12.0

- old
+ new

@@ -59,33 +59,33 @@ # # h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" } # h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] } def deep_merge(other_hash) dup.deep_merge!(other_hash) - end unless method_defined? :deep_merge + end alias :+ :deep_merge # Same as +deep_merge+, but modifies +self+. def deep_merge!(other_hash) other_hash.each_pair do |k,v| tv = self[k] self[k] = tv.is_a?(::Hash) && v.is_a?(::Hash) ? tv.deep_merge(v) : v end self - end unless method_defined? :deep_merge! + end # return bool that does the sub hash all element include the hash who call this or not def deep_include?(sub_hash) sub_hash.keys.all? do |key| self.has_key?(key) && if sub_hash[key].is_a?(::Hash) self[key].is_a?(::Hash) && self[key].deep_include?(sub_hash[key]) else self[key] == sub_hash[key] end end - end unless method_defined? :deep_include? + end # map hash will work just alike map but instead of an array it will return a hash obj # # {:hello=> "world",:world => "hello"}.map_hash{|k,v| [ k , 123] } # #> {:hello=>123, :world=>123} @@ -120,13 +120,18 @@ alias :map2hash :map_hash def map_hash! *args,&block self.replace(self.map_hash(*args,&block)) end - alias :map2hash! :map_hash! + # check if every hash pair included in the self hash obj + def include_hash? hash + return super unless hash.class <= ::Hash + return (hash.to_a - self.to_a).empty? + end + alias :contain_hash? :include_hash? # Fetch a nested hash value def value_by_keys(*attrs) attr_count = attrs.size current_val = self @@ -136,9 +141,10 @@ return nil if current_val[attr_name].nil? current_val = current_val[attr_name] end return nil end + alias :fetch_by_keys :value_by_keys end end \ No newline at end of file