Module: IndiferentHash

Defined in:
lib/rbbt/util/misc/indiferent_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(hash) ⇒ Object



3
4
5
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 3

def self.setup(hash)
  hash.extend IndiferentHash 
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 22

def [](key)
  res = super(key) 
  return res unless res.nil?

  case key
  when Symbol, Module
    super(key.to_s)
  when String
    super(key.to_sym)
  else
    super(key)
  end
end

#[]=(key, value) ⇒ Object



17
18
19
20
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 17

def []=(key,value)
  delete(key)
  super(key,value)
end

#delete(key) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 51

def delete(key)
  case key
  when Symbol, Module
    super(key) || super(key.to_s)
  when String
    super(key) || super(key.to_sym)
  else
    super(key)
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 40

def include?(key)
  case key
  when Symbol, Module
    super(key) || super(key.to_s)
  when String
    super(key) || super(key.to_sym)
  else
    super(key)
  end
end

#merge(other) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 7

def merge(other)
  new = self.dup
  IndiferentHash.setup(new)
  other.each do |k,value|
    new.delete k
    new[k] = value
  end
  new
end

#values_at(*key_list) ⇒ Object



36
37
38
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 36

def values_at(*key_list)
  key_list.inject([]){|acc,key| acc << self[key]}
end