Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/midwire_common/hash.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#apply_diff(changes, direction = :right) ⇒ Object
TODO: Spec/Test this method rubocop:disable Metrics/AbcSize.
-
#apply_diff!(changes, direction = :right) ⇒ Object
TODO: Spec/Test this method.
- #bottomless ⇒ Object
- #diff(other) ⇒ Object
-
#except(*keys) ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}.
- #grep(pattern) ⇒ Object
-
#only(*keys) ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.only(:a) -> => 1.
-
#pop(*keys) ⇒ Object
Usage h = { :a => 1, :b => 2, :c => 3}.pop(:a) -> => 1 …
-
#recursively_symbolize_keys! ⇒ Object
Recursively change keys to symbols Modifies the hash keys in place.
-
#symbolize_keys ⇒ Object
rubocop:enable Style/RescueModifier.
-
#symbolize_keys! ⇒ Object
rubocop:disable Style/RescueModifier.
-
#to_query_string ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.to_query_string #= a=1&b=2&c=3.
Instance Method Details
#apply_diff(changes, direction = :right) ⇒ Object
TODO: Spec/Test this method rubocop:disable Metrics/AbcSize
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/midwire_common/hash.rb', line 55 def apply_diff(changes, direction = :right) cloned = clone path = [[cloned, changes]] pos, local_changes = path.pop while local_changes local_changes.each_pair do |key, change| if change.is_a?(Array) pos[key] = (direction == :right) ? change[1] : change[0] else pos[key] = pos[key].clone path.push([pos[key], change]) end end pos, local_changes = path.pop end cloned end |
#apply_diff!(changes, direction = :right) ⇒ Object
TODO: Spec/Test this method
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/midwire_common/hash.rb', line 37 def apply_diff!(changes, direction = :right) path = [[self, changes]] pos, local_changes = path.pop while local_changes local_changes.each_pair do |key, change| if change.is_a?(Array) pos[key] = (direction == :right) ? change[1] : change[0] else path.push([pos[key], change]) end end pos, local_changes = path.pop end self end |
#bottomless ⇒ Object
12 13 14 |
# File 'lib/midwire_common/hash.rb', line 12 def bottomless BottomlessHash.from_hash(self) end |
#diff(other) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/midwire_common/hash.rb', line 23 def diff(other) (keys + other.keys).uniq.each_with_object({}) do |key, memo| unless self[key] == other[key] if self[key].is_a?(Hash) && other[key].is_a?(Hash) memo[key] = self[key].diff(other[key]) else memo[key] = [self[key], other[key]] end end memo end end |
#except(*keys) ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
75 76 77 |
# File 'lib/midwire_common/hash.rb', line 75 def except(*keys) reject { |k, _v| keys.flatten.include? k.to_sym } end |
#grep(pattern) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/midwire_common/hash.rb', line 16 def grep(pattern) each_with_object([]) do |kv, res| res << kv if kv[0] =~ pattern || kv[1] =~ pattern res end end |
#only(*keys) ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.only(:a) -> => 1
80 81 82 |
# File 'lib/midwire_common/hash.rb', line 80 def only(*keys) dup.reject { |k, _v| !keys.flatten.include? k.to_sym } end |
#pop(*keys) ⇒ Object
Usage h = { :a => 1, :b => 2, :c => 3}.pop(:a) -> => 1 … and now h == { :b => 2, :c => 3}
86 87 88 89 90 |
# File 'lib/midwire_common/hash.rb', line 86 def pop(*keys) r = reject { |k, _v| !keys.flatten.include? k.to_sym } self.reject! { |k, _v| keys.flatten.include? k.to_sym } r end |
#recursively_symbolize_keys! ⇒ Object
Recursively change keys to symbols Modifies the hash keys in place.
115 116 117 118 119 120 121 |
# File 'lib/midwire_common/hash.rb', line 115 def recursively_symbolize_keys! self.symbolize_keys! values.each do |v| v.recursively_symbolize_keys! if v.is_a? Hash end self end |
#symbolize_keys ⇒ Object
rubocop:enable Style/RescueModifier
109 110 111 |
# File 'lib/midwire_common/hash.rb', line 109 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object
rubocop:disable Style/RescueModifier
101 102 103 104 105 106 |
# File 'lib/midwire_common/hash.rb', line 101 def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self end |
#to_query_string ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.to_query_string #= a=1&b=2&c=3
93 94 95 96 97 98 |
# File 'lib/midwire_common/hash.rb', line 93 def to_query_string require 'uri' map do |k, v| "#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}" end.join('&') end |