Class: Hash::WithIndifferentAccess

Inherits:
Hash show all
Defined in:
lib/olelo/extensions.rb

Overview

Stolen from rails

Instance Method Summary collapse

Methods inherited from Hash

#with_indifferent_access, with_indifferent_access

Constructor Details

#initialize(arg = {}) ⇒ WithIndifferentAccess

Returns a new instance of WithIndifferentAccess.



60
61
62
63
64
65
66
67
# File 'lib/olelo/extensions.rb', line 60

def initialize(arg = {})
  if Hash === arg
    super()
    update(arg)
  else
    super(arg)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



81
82
83
84
# File 'lib/olelo/extensions.rb', line 81

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

#default(key = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/olelo/extensions.rb', line 73

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

#delete(key) ⇒ Object



117
118
119
# File 'lib/olelo/extensions.rb', line 117

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

#dupObject



109
110
111
# File 'lib/olelo/extensions.rb', line 109

def dup
  WithIndifferentAccess.new(self)
end

#fetch(key, *extras) ⇒ Object



101
102
103
# File 'lib/olelo/extensions.rb', line 101

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

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/olelo/extensions.rb', line 93

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

#merge(hash) ⇒ Object



113
114
115
# File 'lib/olelo/extensions.rb', line 113

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

#regular_updateObject



71
# File 'lib/olelo/extensions.rb', line 71

alias_method :regular_update, :update

#regular_writerObject



70
# File 'lib/olelo/extensions.rb', line 70

alias_method :regular_writer, :[]=

#to_hashObject



121
122
123
# File 'lib/olelo/extensions.rb', line 121

def to_hash
  Hash.new(default).merge(self)
end

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



86
87
88
89
# File 'lib/olelo/extensions.rb', line 86

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

#values_at(*indices) ⇒ Object



105
106
107
# File 'lib/olelo/extensions.rb', line 105

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