Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/usergrid/extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#add_dot_notation!(_hash = self) ⇒ Object

recursive



21
22
23
24
25
26
27
28
29
# File 'lib/usergrid/extensions/hash.rb', line 21

def add_dot_notation!(_hash=self)
  _hash.each do |k,v|
    getter = k.to_sym; setter = "#{k}=".to_sym
    _hash.define_singleton_method getter, lambda { _hash[k] } unless _hash.respond_to? getter
    _hash.define_singleton_method setter, lambda { |v| _hash[k] = v } unless _hash.respond_to? setter
    add_dot_notation!(v) if v.is_a? Hash
    v.each { |e| add_dot_notation!(e) if e.is_a? Hash } if v.is_a? Array
  end
end

#except(*keys) ⇒ Object



11
12
13
# File 'lib/usergrid/extensions/hash.rb', line 11

def except(*keys)
  dup.except! *keys
end

#except!(*keys) ⇒ Object



15
16
17
18
# File 'lib/usergrid/extensions/hash.rb', line 15

def except!(*keys)
  keys.each { |key| key.is_a?(Array) ? except!(*key) : delete(key) }
  self
end

#symbolize_keysObject

not recursive



4
5
6
7
8
9
# File 'lib/usergrid/extensions/hash.rb', line 4

def symbolize_keys
  inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end