Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/scimitar/support/hash_with_indifferent_case_insensitive_access.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deep_indifferent_case_insensitive_access(object) ⇒ Object

Supports #with_indifferent_case_insensitive_access. Converts the given item to indifferent, case-insensitive access as a Hash; or converts Array items if given an Array; or returns the given object.

Hashes and Arrays at all depths are duplicated as a result.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scimitar/support/hash_with_indifferent_case_insensitive_access.rb', line 24

def self.deep_indifferent_case_insensitive_access(object)
  if object.is_a?(Hash)
    new_hash = Scimitar::Support::HashWithIndifferentCaseInsensitiveAccess.new
    object.each do | key, value |
      new_hash[key] = deep_indifferent_case_insensitive_access(value)
    end
    new_hash

  elsif object.is_a?(Array)
    object.map do | array_entry |
      deep_indifferent_case_insensitive_access(array_entry)
    end

  else
    object

  end
end

Instance Method Details

#with_indifferent_case_insensitive_accessObject

Converts this Hash to an instance of Scimitar::Support::HashWithIndifferentCaseInsensitiveAccess, which is a subclass of ActiveSupport::HashWithIndifferentAccess with the addition of case-insensitive lookup.

Note that this is more thorough than the ActiveSupport counterpart. It converts recursively, so that all Hashes to arbitrary depth, including any hashes inside Arrays, are converted. This is an expensive operation.



14
15
16
# File 'lib/scimitar/support/hash_with_indifferent_case_insensitive_access.rb', line 14

def with_indifferent_case_insensitive_access
  self.class.deep_indifferent_case_insensitive_access(self)
end