Module: Hanami::Utils::Hash
- Extended by:
- Dry::Transformer::Registry
- Defined in:
- lib/hanami/utils/hash.rb
Overview
Hash transformations
Class Method Summary collapse
-
.deep_dup(input) ⇒ ::Hash
Deep duplicates hash values.
-
.deep_serialize(input) ⇒ ::Hash
Deep serializes given object into a ‘Hash`.
-
.deep_stringify(input) ⇒ ::Hash
Deeply stringifies the given hash.
-
.deep_symbolize(input) ⇒ ::Hash
Performs deep symbolize on the given hash.
-
.stringify(input) ⇒ ::Hash
Stringifies the given hash.
-
.symbolize(input) ⇒ ::Hash
Symbolize the given hash.
Class Method Details
.deep_dup(input) ⇒ ::Hash
Deep duplicates hash values
The output of this function is a deep duplicate of the input. Any further modification on the input, won’t be reflected on the output and viceversa.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hanami/utils/hash.rb', line 134 def self.deep_dup(input) input.transform_values do |v| case v when ::Hash deep_dup(v) else v.dup end end end |
.deep_serialize(input) ⇒ ::Hash
Deep serializes given object into a ‘Hash`
Please note that the returning ‘Hash` will use symbols as keys.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/hanami/utils/hash.rb', line 169 def self.deep_serialize(input) 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 stringifies the given hash
93 94 95 |
# File 'lib/hanami/utils/hash.rb', line 93 def self.deep_stringify(input) self[:deep_stringify_keys].call(input) end |
.deep_symbolize(input) ⇒ ::Hash
Performs deep symbolize on the given hash
53 54 55 |
# File 'lib/hanami/utils/hash.rb', line 53 def self.deep_symbolize(input) self[:deep_symbolize_keys].call(input) end |
.stringify(input) ⇒ ::Hash
Stringifies the given hash
73 74 75 |
# File 'lib/hanami/utils/hash.rb', line 73 def self.stringify(input) self[:stringify_keys].call(input) end |
.symbolize(input) ⇒ ::Hash
Symbolize the given hash
31 32 33 |
# File 'lib/hanami/utils/hash.rb', line 31 def self.symbolize(input) self[:symbolize_keys].call(input) end |