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



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 26

def [](key)
  res = super(key) 
  return res unless res.nil? or (_default? and not keys.include? key)

  case key
  when Symbol, Module
    super(key.to_s)
  when String
    super(key.to_sym)
  else
    res
  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

#_default?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 22

def _default?
  @_default ||= self.default or self.default_proc
end

#delete(key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 55

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)


44
45
46
47
48
49
50
51
52
53
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 44

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



40
41
42
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 40

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