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 rubocop:disable Metrics/ClassLength
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_serialize(input) ⇒ ::Hash
Deep serialize given object into a ‘Hash`.
-
.deep_stringify(input) ⇒ ::Hash
Deeply stringify the given hash.
-
.deep_symbolize(input) ⇒ ::Hash
Deep symbolize the given hash.
-
.stringify(input) ⇒ ::Hash
Stringify the given hash.
-
.symbolize(input) ⇒ ::Hash
Symbolize the given hash.
Instance Method Summary collapse
- #==(other) ⇒ TrueClass, FalseClass (also: #eql?) deprecated Deprecated.
- #[](key) ⇒ Object? deprecated Deprecated.
- #[]=(key, value) ⇒ Object deprecated Deprecated.
-
#deep_dup ⇒ Hash
deprecated
Deprecated.
Use Hash.deep_dup
- #deep_symbolize! ⇒ Hash deprecated Deprecated.
- #delete(key) ⇒ Object? deprecated Deprecated.
- #hash ⇒ Fixnum deprecated Deprecated.
- #initialize(hash = {}, &blk) ⇒ Hanami::Utils::Hash constructor deprecated Deprecated.
- #inspect ⇒ String deprecated Deprecated.
- #keys ⇒ Array deprecated Deprecated.
-
#method_missing(method_name, *args, &blk) ⇒ Object
private
Override Ruby’s method_missing in order to provide ::Hash interface.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
private
Override Ruby’s respond_to_missing? in order to support ::Hash interface.
-
#stringify! ⇒ Hash
deprecated
Deprecated.
Use Hash.stringify
-
#symbolize! ⇒ Hash
deprecated
Deprecated.
Use Hash.symbolize
- #to_a ⇒ ::Array deprecated Deprecated.
- #to_h ⇒ ::Hash (also: #to_hash) deprecated Deprecated.
Constructor Details
#initialize(hash = {}, &blk) ⇒ Hanami::Utils::Hash
Initialize the hash
237 238 239 240 |
# File 'lib/hanami/utils/hash.rb', line 237 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(method_name, *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
507 508 509 510 511 512 513 |
# File 'lib/hanami/utils/hash.rb', line 507 def method_missing(method_name, *args, &blk) raise NoMethodError.new(%(undefined method `#{method_name}' for #{@hash}:#{self.class})) unless respond_to?(method_name) h = @hash.__send__(method_name, *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.
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/hanami/utils/hash.rb', line 161 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_serialize(input) ⇒ ::Hash
Deep serialize given object into a ‘Hash`
Please note that the returning ‘Hash` will use symbols as keys.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/hanami/utils/hash.rb', line 196 def self.deep_serialize(input) # rubocop:disable Metrics/MethodLength input.to_hash.each_with_object({}) do |(key, value), output| output[key.to_sym] = case value when ->(h) { h.respond_to?(:to_hash) } deep_serialize(value) when Array value.map do |item| item.respond_to?(:to_hash) ? deep_serialize(item) : item end else value end end end |
.deep_stringify(input) ⇒ ::Hash
Deeply stringify the given hash
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/hanami/utils/hash.rb', line 108 def self.deep_stringify(input) # rubocop:disable Metrics/MethodLength input.each_with_object({}) do |(key, value), output| output[key.to_s] = case value when ::Hash deep_stringify(value) when Array value.map do |item| item.is_a?(::Hash) ? deep_stringify(item) : item end else value end end end |
.deep_symbolize(input) ⇒ ::Hash
Deep symbolize the given hash
68 69 70 |
# File 'lib/hanami/utils/hash.rb', line 68 def self.deep_symbolize(input) self[:deep_symbolize_keys].call(input) end |
.stringify(input) ⇒ ::Hash
Stringify the given hash
88 89 90 |
# File 'lib/hanami/utils/hash.rb', line 88 def self.stringify(input) self[:stringify_keys].call(input) end |
.symbolize(input) ⇒ ::Hash
Symbolize the given hash
46 47 48 |
# File 'lib/hanami/utils/hash.rb', line 46 def self.symbolize(input) self[:symbolize_keys].call(input) end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Equality
475 476 477 |
# File 'lib/hanami/utils/hash.rb', line 475 def ==(other) @hash == other.to_h end |
#[](key) ⇒ Object?
Retrieves the value object corresponding to the key object.
423 424 425 |
# File 'lib/hanami/utils/hash.rb', line 423 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
Associates the value given by value with the key given by key.
436 437 438 |
# File 'lib/hanami/utils/hash.rb', line 436 def []=(key, value) @hash[key] = value end |
#deep_dup ⇒ Hash
Use deep_dup
Return a deep copy of the current Hanami::Utils::Hash
380 381 382 383 384 |
# File 'lib/hanami/utils/hash.rb', line 380 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
Use deep_symbolize
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/hanami/utils/hash.rb', line 281 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.
409 410 411 |
# File 'lib/hanami/utils/hash.rb', line 409 def delete(key) @hash.delete(key) end |
#hash ⇒ Fixnum
Returns the hash of the internal @hash
487 488 489 |
# File 'lib/hanami/utils/hash.rb', line 487 def hash @hash.hash end |
#inspect ⇒ String
Returns a string describing the internal @hash
497 498 499 |
# File 'lib/hanami/utils/hash.rb', line 497 def inspect @hash.inspect end |
#keys ⇒ Array
Returns a new array populated with the keys from this hash
394 395 396 |
# File 'lib/hanami/utils/hash.rb', line 394 def keys @hash.keys end |
#respond_to_missing?(method_name, 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
519 520 521 |
# File 'lib/hanami/utils/hash.rb', line 519 def respond_to_missing?(method_name, include_private = false) @hash.respond_to?(method_name, include_private) end |
#stringify! ⇒ Hash
Use stringify
Convert in-place all the keys to Symbol instances, nested hashes are converted too.
307 308 309 310 311 312 313 314 315 316 |
# File 'lib/hanami/utils/hash.rb', line 307 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
Use symbolize
Convert in-place all the keys to Symbol instances.
257 258 259 260 261 262 263 264 |
# File 'lib/hanami/utils/hash.rb', line 257 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.
465 466 467 |
# File 'lib/hanami/utils/hash.rb', line 465 def to_a @hash.to_a end |
#to_h ⇒ ::Hash Also known as: to_hash
Returns a Ruby Hash as duplicated version of self
448 449 450 451 452 453 |
# File 'lib/hanami/utils/hash.rb', line 448 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 |