Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/midwire_common/hash.rb
Instance Method Summary collapse
- #apply_diff(changes, direction = :right) ⇒ Object
- #apply_diff!(changes, direction = :right) ⇒ 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
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
-
#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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/midwire_common/hash.rb', line 38 def apply_diff(changes, direction = :right) cloned = self.clone path = [[cloned, changes]] pos, local_changes = path.pop while local_changes local_changes.each_pair {|key, change| if change.kind_of?(Array) pos[key] = (direction == :right) ? change[1] : change[0] else pos[key] = pos[key].clone path.push([pos[key], change]) end } pos, local_changes = path.pop end cloned end |
#apply_diff!(changes, direction = :right) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/midwire_common/hash.rb', line 22 def apply_diff!(changes, direction = :right) path = [[self, changes]] pos, local_changes = path.pop while local_changes local_changes.each_pair {|key, change| if change.kind_of?(Array) pos[key] = (direction == :right) ? change[1] : change[0] else path.push([pos[key], change]) end } pos, local_changes = path.pop end self end |
#diff(other) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/midwire_common/hash.rb', line 9 def diff(other) (self.keys + other.keys).uniq.inject({}) do |memo, key| unless self[key] == other[key] if self[key].kind_of?(Hash) && other[key].kind_of?(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}
57 58 59 60 61 |
# File 'lib/midwire_common/hash.rb', line 57 def except(*keys) self.reject { |k,v| keys.flatten.include? k.to_sym } end |
#grep(pattern) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/midwire_common/hash.rb', line 2 def grep(pattern) inject([]) do |res, kv| res << kv if kv[0] =~ pattern or kv[1] =~ pattern res end end |
#only(*keys) ⇒ Object
Usage { :a => 1, :b => 2, :c => 3}.only(:a) -> => 1
64 65 66 67 68 |
# File 'lib/midwire_common/hash.rb', line 64 def only(*keys) self.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}
72 73 74 75 76 77 78 79 80 |
# File 'lib/midwire_common/hash.rb', line 72 def pop(*keys) r = self.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
101 102 103 104 105 106 107 108 109 |
# File 'lib/midwire_common/hash.rb', line 101 def recursively_symbolize_keys! self.symbolize_keys! self.values.each do |v| if v.is_a? Hash v.recursively_symbolize_keys! end end self end |
#symbolize_keys ⇒ Object
97 98 99 |
# File 'lib/midwire_common/hash.rb', line 97 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object
90 91 92 93 94 95 |
# File 'lib/midwire_common/hash.rb', line 90 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
83 84 85 86 87 88 |
# File 'lib/midwire_common/hash.rb', line 83 def to_query_string require 'uri' collect do |k, v| "#{URI.encode(k.to_s)}=#{URI.encode(v.to_s)}" end.join("&") end |