Class: Wrest::HashWithCaseInsensitiveAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/wrest/hash_with_case_insensitive_access.rb

Overview

A hash with case-insensitive key access.

hash = Wrest::HashWithCaseInsensitiveAccess.new 'Abcd' => 1, 'xyz' => 2

hash['abcd']  #=> 1
hash['aBCd'] #=> 1

Instance Method Summary collapse

Methods included from CoreExt::Hash::Conversions

#mutate_using

Constructor Details

#initialize(hash = {}) ⇒ HashWithCaseInsensitiveAccess

:nodoc:



12
13
14
15
16
17
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 12

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

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 19

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

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 23

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

#delete(key) ⇒ Object



27
28
29
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 27

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

#merge(other) ⇒ Object



35
36
37
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 35

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

#merge!(other) ⇒ Object



39
40
41
42
43
44
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 39

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

#values_at(*indices) ⇒ Object



31
32
33
# File 'lib/wrest/hash_with_case_insensitive_access.rb', line 31

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