Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandro/support/hash.rb

Overview

Constant Summary collapse

HASH_MERGER =

deep_merge_hash! by Stefan Rusterholz, see www.ruby-forum.com/topic/142809

proc do |key, v1, v2|
  Hash === v1 && Hash === v2 ? v1.merge(v2, &HASH_MERGER) : v2
end

Instance Method Summary collapse

Instance Method Details

#deep_merge!(data) ⇒ Object



26
27
28
# File 'lib/cassandro/support/hash.rb', line 26

def deep_merge!(data)
  merge!(data, &HASH_MERGER)
end

#deep_symbolize_keysObject



13
14
15
16
17
18
19
# File 'lib/cassandro/support/hash.rb', line 13

def deep_symbolize_keys
  inject({}) { |result, (key, value)|
    value = value.deep_symbolize_keys if value.is_a?(Hash)
    result[(key.to_sym rescue key) || key] = value
    result
  }
end

#except(*less_keys) ⇒ Object



9
10
11
# File 'lib/cassandro/support/hash.rb', line 9

def except(*less_keys)
  slice(*keys - less_keys)
end

#slice(*keep_keys) ⇒ Object



3
4
5
6
7
# File 'lib/cassandro/support/hash.rb', line 3

def slice(*keep_keys)
  h = {}
  keep_keys.each { |key| h[key] = fetch(key) }
  h
end