Class: DataMapper::Mash
- Inherits:
-
Hash
- Object
- Hash
- DataMapper::Mash
- Defined in:
- lib/dm-core/support/mash.rb
Overview
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Sets the
value
associated with the specifiedkey
. - #convert_key(key) ⇒ Object protected private
-
#convert_value(value) ⇒ Object
protected
private
The converted value.
-
#default(key = nil) ⇒ Object
Gets the default value for the mash.
- #delete(key) ⇒ Object
-
#except(*keys) ⇒ Mash
Returns a mash that includes everything but the given
keys
. -
#except!(*keys) ⇒ Hash
Removes the specified
keys
from the mash. -
#fetch(key, *extras) ⇒ Object
The value at key or the default value.
-
#initialize(constructor = {}) ⇒ Mash
constructor
Initializes a new mash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
Determines whether the mash contains the specified
key
. -
#merge(hash) ⇒ Mash
A new mash with the hash values merged in.
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
-
#symbolize_keys ⇒ Hash
The mash as a Hash with symbolized keys.
-
#to_hash ⇒ Hash
The mash as a Hash with string keys.
-
#update(other_hash) ⇒ Mash
(also: #merge!)
Updates the mash with the key/value pairs from the specified hash.
-
#values_at(*indices) ⇒ Array
The values at each of the provided keys.
Constructor Details
#initialize(constructor = {}) ⇒ Mash
Initializes a new mash.
12 13 14 15 16 17 18 19 |
# File 'lib/dm-core/support/mash.rb', line 12 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Instance Method Details
#[]=(key, value) ⇒ Object
Sets the value
associated with the specified key
.
42 43 44 |
# File 'lib/dm-core/support/mash.rb', line 42 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#convert_key(key) ⇒ Object (protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
153 154 155 |
# File 'lib/dm-core/support/mash.rb', line 153 def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end |
#convert_value(value) ⇒ Object (protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The converted value. A Hash or an Array of hashes, will be converted to their Mash equivalents.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dm-core/support/mash.rb', line 164 def convert_value(value) if value.class == Hash mash = Mash.new(value) mash.default = value.default mash elsif value.is_a?(Array) value.collect { |e| convert_value(e) } else value end end |
#default(key = nil) ⇒ Object
Gets the default value for the mash.
27 28 29 30 31 32 33 |
# File 'lib/dm-core/support/mash.rb', line 27 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
96 97 98 |
# File 'lib/dm-core/support/mash.rb', line 96 def delete(key) super(convert_key(key)) end |
#except(*keys) ⇒ Mash
Returns a mash that includes everything but the given keys
.
109 110 111 |
# File 'lib/dm-core/support/mash.rb', line 109 def except(*keys) self.dup.except!(*keys.map {|k| convert_key(k)}) end |
#except!(*keys) ⇒ Hash
Removes the specified keys
from the mash.
123 124 125 126 |
# File 'lib/dm-core/support/mash.rb', line 123 def except!(*keys) keys.each { |key| delete(key) } self end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
76 77 78 |
# File 'lib/dm-core/support/mash.rb', line 76 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Determines whether the mash contains the specified key
.
64 65 66 |
# File 'lib/dm-core/support/mash.rb', line 64 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
91 92 93 |
# File 'lib/dm-core/support/mash.rb', line 91 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
36 |
# File 'lib/dm-core/support/mash.rb', line 36 alias_method :regular_update, :update |
#regular_writer ⇒ Object
35 |
# File 'lib/dm-core/support/mash.rb', line 35 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
131 |
# File 'lib/dm-core/support/mash.rb', line 131 def stringify_keys!; self end |
#symbolize_keys ⇒ Hash
Returns The mash as a Hash with symbolized keys.
134 135 136 137 138 |
# File 'lib/dm-core/support/mash.rb', line 134 def symbolize_keys h = Hash.new(default) each { |key, val| h[key.to_sym] = val } h end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
141 142 143 |
# File 'lib/dm-core/support/mash.rb', line 141 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Updates the mash with the key/value pairs from the specified hash.
53 54 55 56 |
# File 'lib/dm-core/support/mash.rb', line 53 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end |
#values_at(*indices) ⇒ Array
Returns The values at each of the provided keys.
84 85 86 |
# File 'lib/dm-core/support/mash.rb', line 84 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |