Class: HashWithIndifferentAccess

Inherits:
Hash show all
Defined in:
lib/active_support/core_ext/hash/indifferent_access.rb

Overview

this class has dubious semantics and we only have it so that people can write params instead of params

Constant Summary

Constants included from ActiveSupport::CoreExtensions::Hash::Conversions

ActiveSupport::CoreExtensions::Hash::Conversions::XML_FORMATTING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::Hash::Diff

#diff

Methods included from ActiveSupport::CoreExtensions::Hash::Conversions

included, #to_xml

Methods included from ActiveSupport::CoreExtensions::Hash::ReverseMerge

#reverse_merge, #reverse_merge!

Methods included from ActiveSupport::CoreExtensions::Hash::IndifferentAccess

#with_indifferent_access

Methods included from ActiveSupport::CoreExtensions::Hash::Keys

#assert_valid_keys, #stringify_keys, #symbolize_keys

Constructor Details

#initialize(constructor = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



5
6
7
8
9
10
11
12
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 5

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 25

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 14

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object



60
61
62
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 60

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

#dupObject



52
53
54
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 52

def dup
  HashWithIndifferentAccess.new(self)
end

#fetch(key, *extras) ⇒ Object



44
45
46
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 44

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

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 36

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

#merge(hash) ⇒ Object



56
57
58
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 56

def merge(hash)
  self.dup.update(hash)
end

#regular_updateObject



23
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 23

alias_method :regular_update, :update

#regular_writerObject



22
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 22

alias_method :regular_writer, :[]=

#stringify_keys!Object



64
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 64

def stringify_keys!; self end

#symbolize_keys!Object



65
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 65

def symbolize_keys!; self end

#update(other_hash) ⇒ Object Also known as: merge!



29
30
31
32
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 29

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end

#values_at(*indices) ⇒ Object



48
49
50
# File 'lib/active_support/core_ext/hash/indifferent_access.rb', line 48

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