Class: Fleck::HashWithIndifferentAccess

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

Overview

‘HashWithIndifferentAccess` extends `Hash` class and adds the possibility to access values by using both string and symbol keys indifferently.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#filter!, #filtered!, #to_hash_with_indifferent_access, #to_s

Constructor Details

#initialize(original) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



7
8
9
10
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 7

def initialize(original)
  super(nil)
  copy_from(original)
end

Class Method Details

.convert_value(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 28

def self.convert_value(value)
  case value
  when Hash
    value.to_hash_with_indifferent_access
  when Array
    value.map! { |item| item.is_a?(Hash) || item.is_a?(Array) ? Fleck::HashWithIndifferentAccess.convert_value(item) : item }
  else
    value
  end
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 16

def [](key)
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



12
13
14
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 12

def []=(key, value)
  super(key.to_s, self.class.convert_value(value))
end

#delete(key) ⇒ Object



24
25
26
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 24

def delete(key)
  super(key.to_s)
end

#fetch(key, *extras) ⇒ Object



20
21
22
# File 'lib/fleck/utilities/hash_with_indifferent_access.rb', line 20

def fetch(key, *extras)
  super(key.to_s, *extras)
end