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



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

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



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

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

#_default?Boolean

Returns:

  • (Boolean)


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

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

#clean_versionObject



65
66
67
68
69
70
71
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 65

def clean_version
  clean = {}
  each do |k,v|
    clean[k.to_s] = v unless clean.include? k.to_s
  end
  clean
end

#delete(key) ⇒ Object



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

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)


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

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
# 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[k] = value
  end
  new
end

#values_at(*key_list) ⇒ Object



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

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