Module: MPatch::Include::Hash
- Defined in:
- lib/mpatch/hash.rb
Instance Method Summary collapse
-
#deep_include?(sub_hash) ⇒ Boolean
return bool that does the sub hash all element include the hash who call this or not.
-
#deep_merge(other_hash) ⇒ Object
(also: #+)
Returns a new hash with
self
andother_hash
merged recursively. -
#deep_merge!(other_hash) ⇒ Object
Same as
deep_merge
, but modifiesself
. -
#include_hash?(hash) ⇒ Boolean
(also: #contain_hash?)
check if every hash pair included in the self hash obj.
-
#map_hash(*args, &block) ⇒ Object
(also: #map2hash)
map hash will work just alike map but instead of an array it will return a hash obj.
- #map_hash!(*args, &block) ⇒ Object (also: #map2hash!)
-
#remove(*keys) ⇒ Object
non-destructive version.
-
#remove!(*keys) ⇒ Object
pass single or array of keys, which will be removed, returning the remaining hash.
-
#trim(*args) ⇒ Object
remove elements by keys, array of keys, hashTags, strings.
-
#value_by_keys(*attrs) ⇒ Object
(also: #fetch_by)
Fetch a nested hash value.
Instance Method Details
#deep_include?(sub_hash) ⇒ Boolean
return bool that does the sub hash all element include the hash who call this or not
78 79 80 81 82 83 84 85 86 |
# File 'lib/mpatch/hash.rb', line 78 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 |
#deep_merge(other_hash) ⇒ Object Also known as: +
Returns a new hash with self
and other_hash
merged recursively.
h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
62 63 64 |
# File 'lib/mpatch/hash.rb', line 62 def deep_merge(other_hash) dup.deep_merge!(other_hash) end |
#deep_merge!(other_hash) ⇒ Object
Same as deep_merge
, but modifies self
.
69 70 71 72 73 74 75 |
# File 'lib/mpatch/hash.rb', line 69 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 |
#include_hash?(hash) ⇒ Boolean Also known as: contain_hash?
check if every hash pair included in the self hash obj
128 129 130 131 |
# File 'lib/mpatch/hash.rb', line 128 def include_hash? hash return super unless hash.class <= ::Hash return (hash.to_a - self.to_a).empty? end |
#map_hash(*args, &block) ⇒ Object Also known as: map2hash
map hash will work just alike map but instead of an array it will return a hash obj
“world”,:world => “hello”.map_hash{|k,v| [ k , 123] } #> :world=>123
“world”,:world => “hello”.map_hash{|k,v| { k => 123 } } #> :world=>123
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mpatch/hash.rb', line 96 def map_hash *args,&block tmp_hash= self.class.new(*args) self.map(&block).each do |hash| case true when hash.class <= ::Array tmp_hash.deep_merge!( self.class[*hash] ) when hash.class <= ::Hash tmp_hash.deep_merge!(hash) else raise ArgumentError, "invalid input as last valie for #{__method__}: #{hash}/#{hash.class}" end end return tmp_hash end |
#map_hash!(*args, &block) ⇒ Object Also known as: map2hash!
122 123 124 |
# File 'lib/mpatch/hash.rb', line 122 def map_hash! *args,&block self.replace(self.map_hash(*args,&block)) end |
#remove(*keys) ⇒ Object
non-destructive version
51 52 53 |
# File 'lib/mpatch/hash.rb', line 51 def remove(*keys) self.dup.remove!(*keys) end |
#remove!(*keys) ⇒ Object
pass single or array of keys, which will be removed, returning the remaining hash
45 46 47 48 |
# File 'lib/mpatch/hash.rb', line 45 def remove!(*keys) keys.each{|key| self.delete(key) } self end |
#trim(*args) ⇒ Object
remove elements by keys, array of keys, hashTags, strings
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mpatch/hash.rb', line 11 def trim(*args) args.each do |one_element| case true when one_element.class <= ::Hash begin one_element.each do |key,value| if self[key] == value self.delete(key) end end end when one_element.class <= ::Array begin one_element.each do |one_key| self.delete(one_key) end end when one_element.class <= ::Symbol, one_element.class <= ::String begin self.delete(one_element) end end end return self end |
#value_by_keys(*attrs) ⇒ Object Also known as: fetch_by
Fetch a nested hash value
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mpatch/hash.rb', line 135 def value_by_keys(*attrs) attr_count = attrs.size current_val = self for i in 0..(attr_count-1) attr_name = attrs[i] return current_val[attr_name] if i == (attr_count-1) return nil if current_val[attr_name].nil? current_val = current_val[attr_name] end return nil end |