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, *args) ⇒ Object



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

def [](key, *args)
  res = super(key, *args) 
  res = IndiferentHash.setup(res) if Hash === res
  return res unless res.nil? or (_default? and not keys.include? key)

  case key
  when Symbol, Module
    res = super(key.to_s, *args)
  when String
    res = super(key.to_sym, *args)
  end

  res = IndiferentHash.setup(res) if Hash === res

  res
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



68
69
70
71
72
73
74
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 68

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



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

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)


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

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



42
43
44
# File 'lib/rbbt/util/misc/indiferent_hash.rb', line 42

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