Class: Nitlink::HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/nitlink/hash_with_indifferent_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashWithIndifferentAccess



3
4
5
6
7
8
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 3

def initialize(hash = {})
  super()
  hash.each do |key, value|
    self[convert_key(key)] = value
  end
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 10

def [](key)
  super(convert_key(key))
end

#[]=(key, value) ⇒ Object



14
15
16
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 14

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

#delete(key) ⇒ Object



18
19
20
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 18

def delete(key)
  super(convert_key(key))
end

#fetch(key, *args) ⇒ Object



22
23
24
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 22

def fetch(key, *args)
  super(convert_key(key), *args)
end

#key?(key) ⇒ Boolean



26
27
28
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 26

def key?(key)
  super(convert_key(key))
end

#merge(other) ⇒ Object



34
35
36
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 34

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object



38
39
40
41
42
43
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 38

def merge!(other)
  other.each do |key, value|
    self[convert_key(key)] = value
  end
  self
end

#replace(other_hash) ⇒ Object



53
54
55
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 53

def replace(other_hash)
  super(other_hash)
end

#reverse_merge(other) ⇒ Object



45
46
47
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 45

def reverse_merge(other)
  self.class.new(other).merge(self)
end

#reverse_merge!(other_hash) ⇒ Object



49
50
51
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 49

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#to_hashObject

Convert to a Hash with String keys.



58
59
60
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 58

def to_hash
  Hash.new(default).merge!(self)
end

#values_at(*indices) ⇒ Object



30
31
32
# File 'lib/nitlink/hash_with_indifferent_access.rb', line 30

def values_at(*indices)
  indices.map { |key| self[convert_key(key)] }
end