Class: Hanami::Utils::Hash
- Inherits:
-
Object
- Object
- Hanami::Utils::Hash
- Extended by:
- Transproc::Registry
- Defined in:
- lib/hanami/utils/hash.rb
Overview
Hash on steroids
Constant Summary collapse
- DUPLICATE_LOGIC =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
proc do |value| case value when Hash value.deep_dup when ::Hash Hash.new(value).deep_dup.to_h end end.freeze
Class Method Summary collapse
-
.deep_dup(input) ⇒ ::Hash
Deep duplicate hash values.
-
.deep_symbolize(input) ⇒ ::Hash
Deep symbolize the given hash.
-
.symbolize(input) ⇒ ::Hash
Symbolize the given hash.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Equality.
-
#[](key) ⇒ Object?
Retrieves the value object corresponding to the key object.
-
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
-
#deep_dup ⇒ Hash
Return a deep copy of the current Hanami::Utils::Hash.
-
#deep_symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
-
#delete(key) ⇒ Object?
Deletes the key-value pair and returns the value from hsh whose key is equal to key.
-
#hash ⇒ Fixnum
Returns the hash of the internal @hash.
-
#initialize(hash = {}, &blk) ⇒ Hanami::Utils::Hash
constructor
Initialize the hash.
-
#inspect ⇒ String
Returns a string describing the internal @hash.
-
#keys ⇒ Array
Returns a new array populated with the keys from this hash.
-
#method_missing(m, *args, &blk) ⇒ Object
private
Override Ruby’s method_missing in order to provide ::Hash interface.
-
#respond_to_missing?(m, include_private = false) ⇒ Boolean
private
Override Ruby’s respond_to_missing? in order to support ::Hash interface.
-
#stringify! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
-
#symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances.
-
#to_a ⇒ ::Array
Converts into a nested array of [ key, value ] arrays.
-
#to_h ⇒ ::Hash
(also: #to_hash)
Returns a Ruby Hash as duplicated version of self.
Constructor Details
#initialize(hash = {}, &blk) ⇒ Hanami::Utils::Hash
Initialize the hash
142 143 144 145 |
# File 'lib/hanami/utils/hash.rb', line 142 def initialize(hash = {}, &blk) @hash = hash.to_hash @hash.default_proc = blk if blk end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &blk) ⇒ Object
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.
Override Ruby’s method_missing in order to provide ::Hash interface
399 400 401 402 403 404 405 |
# File 'lib/hanami/utils/hash.rb', line 399 def method_missing(m, *args, &blk) raise NoMethodError.new(%(undefined method `#{m}' for #{@hash}:#{self.class})) unless respond_to?(m) h = @hash.__send__(m, *args, &blk) h = self.class.new(h) if h.is_a?(::Hash) h end |
Class Method Details
.deep_dup(input) ⇒ ::Hash
Deep duplicate hash values
The output of this function is a shallow duplicate of the input. Any further modification on the input, won’t be reflected on the output and viceversa.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/hanami/utils/hash.rb', line 107 def self.deep_dup(input) input.each_with_object({}) do |(k, v), result| result[k] = case v when ::Hash deep_dup(v) else Duplicable.dup(v) end end end |
.deep_symbolize(input) ⇒ ::Hash
Deep symbolize the given hash
66 67 68 |
# File 'lib/hanami/utils/hash.rb', line 66 def self.deep_symbolize(input) self[:deep_symbolize_keys].call(input) end |
.symbolize(input) ⇒ ::Hash
Symbolize the given hash
44 45 46 |
# File 'lib/hanami/utils/hash.rb', line 44 def self.symbolize(input) self[:symbolize_keys].call(input) end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Equality
369 370 371 |
# File 'lib/hanami/utils/hash.rb', line 369 def ==(other) @hash == other.to_h end |
#[](key) ⇒ Object?
Retrieves the value object corresponding to the key object.
321 322 323 |
# File 'lib/hanami/utils/hash.rb', line 321 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
333 334 335 |
# File 'lib/hanami/utils/hash.rb', line 333 def []=(key, value) @hash[key] = value end |
#deep_dup ⇒ Hash
Return a deep copy of the current Hanami::Utils::Hash
281 282 283 284 285 |
# File 'lib/hanami/utils/hash.rb', line 281 def deep_dup self.class.new.tap do |result| @hash.each { |k, v| result[k] = Duplicable.dup(v, &DUPLICATE_LOGIC) } end end |
#deep_symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/hanami/utils/hash.rb', line 184 def deep_symbolize! keys.each do |k| v = delete(k) v = self.class.new(v).deep_symbolize! if v.respond_to?(:to_hash) self[k.to_sym] = v end self end |
#delete(key) ⇒ Object?
Deletes the key-value pair and returns the value from hsh whose key is equal to key.
308 309 310 |
# File 'lib/hanami/utils/hash.rb', line 308 def delete(key) @hash.delete(key) end |
#hash ⇒ Fixnum
Returns the hash of the internal @hash
380 381 382 |
# File 'lib/hanami/utils/hash.rb', line 380 def hash @hash.hash end |
#inspect ⇒ String
Returns a string describing the internal @hash
389 390 391 |
# File 'lib/hanami/utils/hash.rb', line 389 def inspect @hash.inspect end |
#keys ⇒ Array
Returns a new array populated with the keys from this hash
294 295 296 |
# File 'lib/hanami/utils/hash.rb', line 294 def keys @hash.keys end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
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.
Override Ruby’s respond_to_missing? in order to support ::Hash interface
411 412 413 |
# File 'lib/hanami/utils/hash.rb', line 411 def respond_to_missing?(m, include_private = false) @hash.respond_to?(m, include_private) end |
#stringify! ⇒ Hash
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/hanami/utils/hash.rb', line 209 def stringify! keys.each do |k| v = delete(k) v = self.class.new(v).stringify! if v.respond_to?(:to_hash) self[k.to_s] = v end self end |
#symbolize! ⇒ Hash
Convert in-place all the keys to Symbol instances.
161 162 163 164 165 166 167 168 |
# File 'lib/hanami/utils/hash.rb', line 161 def symbolize! keys.each do |k| v = delete(k) self[k.to_sym] = v end self end |
#to_a ⇒ ::Array
Converts into a nested array of [ key, value ] arrays.
360 361 362 |
# File 'lib/hanami/utils/hash.rb', line 360 def to_a @hash.to_a end |
#to_h ⇒ ::Hash Also known as: to_hash
Returns a Ruby Hash as duplicated version of self
344 345 346 347 348 349 |
# File 'lib/hanami/utils/hash.rb', line 344 def to_h @hash.each_with_object({}) do |(k, v), result| v = v.to_h if v.respond_to?(:to_hash) result[k] = v end end |