Class: HashWithIndifferentAccess

Inherits:
Hash
  • Object
show all
Defined in:
lib/cerberus/utils.rb

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge!, #to_http_str

Constructor Details

#initialize(constructor = {}) ⇒ HashWithIndifferentAccess

Returns a new instance of HashWithIndifferentAccess.



93
94
95
96
97
98
99
100
# File 'lib/cerberus/utils.rb', line 93

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

Instance Method Details

#[]=(key, value) ⇒ Object



109
110
111
# File 'lib/cerberus/utils.rb', line 109

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

#default(key) ⇒ Object



102
103
104
# File 'lib/cerberus/utils.rb', line 102

def default(key)
  self[key.to_s] if key.is_a?(Symbol)
end

#delete(key) ⇒ Object



144
145
146
# File 'lib/cerberus/utils.rb', line 144

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

#dupObject



136
137
138
# File 'lib/cerberus/utils.rb', line 136

def dup
  HashWithIndifferentAccess.new(self)
end

#fetch(key, *extras) ⇒ Object



128
129
130
# File 'lib/cerberus/utils.rb', line 128

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

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

Returns:

  • (Boolean)


120
121
122
# File 'lib/cerberus/utils.rb', line 120

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

#merge(hash) ⇒ Object



140
141
142
# File 'lib/cerberus/utils.rb', line 140

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

#regular_updateObject



107
# File 'lib/cerberus/utils.rb', line 107

alias_method :regular_update, :update

#regular_writerObject



106
# File 'lib/cerberus/utils.rb', line 106

alias_method :regular_writer, :[]=

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



113
114
115
116
# File 'lib/cerberus/utils.rb', line 113

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

#values_at(*indices) ⇒ Object



132
133
134
# File 'lib/cerberus/utils.rb', line 132

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