Class: 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.



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

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

Class Method Details

.convert_value(value) ⇒ Object



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

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) ? HashWithIndifferentAccess.convert_value(item) : item }
  else
    value
  end
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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

#fetch(key, *extras) ⇒ Object



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

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