Module: Polyn::Utils::Hash
- Defined in:
- lib/polyn/utils/hash.rb
Overview
Utilities for hash manipulation.
Class Method Summary collapse
-
.deep_camelize_keys(hash) ⇒ ::Hash
Deep camelize keys.
-
.deep_snake_case_keys(hash) ⇒ ::Hash
Deep snake cases keys.
-
.deep_stringify_keys(hash) ⇒ ::Hash
Deep stringifies keys.
-
.deep_symbolize_keys(hash) ⇒ ::Hash
Deep symbolize keys of a hash.
Class Method Details
.deep_camelize_keys(hash) ⇒ ::Hash
Deep camelize keys
61 62 63 64 65 66 |
# File 'lib/polyn/utils/hash.rb', line 61 def self.deep_camelize_keys(hash) hash.each_with_object({}) do |(key, value), result| result[String.to_camel_case(key)] = value.is_a?(::Hash) ? deep_camelize_keys(value) : value end end |
.deep_snake_case_keys(hash) ⇒ ::Hash
Deep snake cases keys
74 75 76 77 78 79 |
# File 'lib/polyn/utils/hash.rb', line 74 def self.deep_snake_case_keys(hash) hash.each_with_object({}) do |(key, value), result| result[String.to_snake_case(key.to_s)] = value.is_a?(::Hash) ? deep_snake_case_keys(value) : value end end |
.deep_stringify_keys(hash) ⇒ ::Hash
Deep stringifies keys
46 47 48 49 50 51 52 53 |
# File 'lib/polyn/utils/hash.rb', line 46 def self.deep_stringify_keys(hash) return hash.map { |item| deep_stringify_keys(item) } if hash.is_a?(::Array) return hash unless hash.is_a?(::Hash) hash.each_with_object({}) do |(key, value), result| result[key.to_s] = deep_stringify_keys(value) end end |
.deep_symbolize_keys(hash) ⇒ ::Hash
Deep symbolize keys of a hash.
31 32 33 34 35 36 37 38 |
# File 'lib/polyn/utils/hash.rb', line 31 def self.deep_symbolize_keys(hash) return hash.map { |item| deep_symbolize_keys(item) } if hash.is_a?(::Array) return hash unless hash.is_a?(::Hash) hash.each_with_object({}) do |(key, value), result| result[key.to_sym] = deep_symbolize_keys(value) end end |