Class: AttributeStruct::Mash
- Inherits:
-
Hash
- Object
- Hash
- AttributeStruct::Mash
- Defined in:
- lib/attribute_struct/attribute_hash.rb
Overview
Class Method Summary collapse
-
.from_hash(hash) ⇒ Mash
The input Hash’s default value is maintained.
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
-
#deep_merge(hash) ⇒ AttributeStruct::Mash
Perform deep merge.
-
#deep_merge!(hash) ⇒ self
Perform deep merge and replace contents of self.
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
-
#except(*keys) ⇒ Mash
A new mash without the selected keys.
-
#fetch(key, *extras) ⇒ Object
The value at key or the default value.
-
#initialize(constructor = {}) ⇒ Mash
constructor
A new instance of Mash.
-
#initialize_copy(orig) ⇒ Object
A new copied Mash.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
True if the key exists in the mash.
-
#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!)
The updated mash.
-
#values_at(*indices) ⇒ Array
The values at each of the provided keys.
Constructor Details
#initialize(constructor = {}) ⇒ Mash
Returns a new instance of Mash.
61 62 63 64 65 66 67 68 |
# File 'lib/attribute_struct/attribute_hash.rb', line 61 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Class Method Details
Instance Method Details
#[]=(key, value) ⇒ Object
110 111 112 |
# File 'lib/attribute_struct/attribute_hash.rb', line 110 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#deep_merge(hash) ⇒ AttributeStruct::Mash
Perform deep merge
243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/attribute_struct/attribute_hash.rb', line 243 def deep_merge(hash) unless (hash.is_a?(Hash)) raise ArgumentError.new "Expecting `Hash` type. Received: `#{hash.class}`" end new_self = self.dup hash.each do |k, v| if (new_self[k].is_a?(Hash) && v.is_a?(Hash)) new_self[k] = new_self[k].deep_merge(v) else new_self[k] = v end end new_self end |
#deep_merge!(hash) ⇒ self
Perform deep merge and replace contents of self
261 262 263 264 |
# File 'lib/attribute_struct/attribute_hash.rb', line 261 def deep_merge!(hash) self.replace(self.deep_merge(hash)) self end |
#default(key = nil) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/attribute_struct/attribute_hash.rb', line 89 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
163 164 165 |
# File 'lib/attribute_struct/attribute_hash.rb', line 163 def delete(key) super(convert_key(key)) end |
#except(*keys) ⇒ Mash
Returns A new mash without the selected keys.
174 175 176 |
# File 'lib/attribute_struct/attribute_hash.rb', line 174 def except(*keys) super(*keys.map { |k| convert_key(k) }) end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
142 143 144 |
# File 'lib/attribute_struct/attribute_hash.rb', line 142 def fetch(key, *extras) super(convert_key(key), *extras) end |
#initialize_copy(orig) ⇒ Object
Returns A new copied Mash.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/attribute_struct/attribute_hash.rb', line 73 def initialize_copy(orig) super # Handle nested values each do |k, v| if v.kind_of?(Mash) || v.is_a?(Array) self[k] = v.dup end end self end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Returns True if the key exists in the mash.
129 130 131 |
# File 'lib/attribute_struct/attribute_hash.rb', line 129 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
157 158 159 |
# File 'lib/attribute_struct/attribute_hash.rb', line 157 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
101 |
# File 'lib/attribute_struct/attribute_hash.rb', line 101 alias_method :regular_update, :update |
#regular_writer ⇒ Object
98 |
# File 'lib/attribute_struct/attribute_hash.rb', line 98 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
181 |
# File 'lib/attribute_struct/attribute_hash.rb', line 181 def stringify_keys!; self end |
#symbolize_keys ⇒ Hash
Returns The mash as a Hash with symbolized keys.
184 185 186 187 188 189 190 191 |
# File 'lib/attribute_struct/attribute_hash.rb', line 184 def symbolize_keys h = Hash.new(default) each do |key, val| key = key.to_sym if key.is_a?(String) || key.is_a?(Symbol) h[key] = val end h end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
194 195 196 |
# File 'lib/attribute_struct/attribute_hash.rb', line 194 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Returns The updated mash.
119 120 121 122 |
# File 'lib/attribute_struct/attribute_hash.rb', line 119 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.
150 151 152 |
# File 'lib/attribute_struct/attribute_hash.rb', line 150 def values_at(*indices) indices.collect { |key| self[convert_key(key)] } end |