Class: Hash
Overview
Hash helpers
Instance Method Summary collapse
- #deep_keys ⇒ Object
-
#deep_merge(other, &block) ⇒ Object
Adapted from ActiveSupport Hash#deep_merge github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/activesupport/lib/active_support/core_ext/hash/deep_merge.rb.
- #deep_sort ⇒ Object
- #deep_transform_keys ⇒ Object
- #deep_transform_keys! ⇒ Object
- #encode64 ⇒ Object
- #strict_encode64 ⇒ Object
- #stringify_names ⇒ Object
- #stringify_names! ⇒ Object
- #symbolize_names ⇒ Object
- #symbolize_names! ⇒ Object
-
#to_deep_struct ⇒ Object
rubocop: disable Metrics/MethodLength.
- #to_dynamodb ⇒ Object
- #to_form ⇒ Object
-
#to_h_from_dynamodb ⇒ Object
rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #to_json_sorted ⇒ Object
- #to_struct ⇒ Object
Instance Method Details
#deep_keys ⇒ Object
6 |
# File 'lib/yake/support/hash.rb', line 6 def deep_keys = map { |k, v| v.respond_to?(:deep_keys) ? [k] + v.deep_keys : k }.flatten |
#deep_merge(other, &block) ⇒ Object
Adapted from ActiveSupport Hash#deep_merge github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/yake/support/hash.rb', line 23 def deep_merge(other, &block) # rubocop: disable Metrics/MethodLength merge(other) do |key, a, b| if a.is_a?(Hash) && b.is_a?(Hash) a.deep_merge(b, &block) elsif a.is_a?(Array) && b.is_a?(Array) a + b elsif block_given? yield key, a, b else b end end end |
#deep_sort ⇒ Object
7 |
# File 'lib/yake/support/hash.rb', line 7 def deep_sort = sort.to_h { |k, v| [k, v.try(:deep_sort) { |x| x }] } |
#deep_transform_keys ⇒ Object
8 |
# File 'lib/yake/support/hash.rb', line 8 def deep_transform_keys(&) = deep_transform(:transform_keys, &) |
#deep_transform_keys! ⇒ Object
9 |
# File 'lib/yake/support/hash.rb', line 9 def deep_transform_keys!(&) = deep_transform(:transform_keys!, &) |
#encode64 ⇒ Object
10 |
# File 'lib/yake/support/hash.rb', line 10 def encode64 = to_json.encode64 |
#strict_encode64 ⇒ Object
11 |
# File 'lib/yake/support/hash.rb', line 11 def strict_encode64 = to_json.strict_encode64 |
#stringify_names ⇒ Object
12 |
# File 'lib/yake/support/hash.rb', line 12 def stringify_names = deep_transform_keys(&:to_s) |
#stringify_names! ⇒ Object
13 |
# File 'lib/yake/support/hash.rb', line 13 def stringify_names! = deep_transform_keys!(&:to_s) |
#symbolize_names ⇒ Object
14 |
# File 'lib/yake/support/hash.rb', line 14 def symbolize_names = deep_transform_keys(&:to_sym) |
#symbolize_names! ⇒ Object
15 |
# File 'lib/yake/support/hash.rb', line 15 def symbolize_names! = deep_transform_keys!(&:to_sym) |
#to_deep_struct ⇒ Object
rubocop: disable Metrics/MethodLength
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yake/support/hash.rb', line 37 def to_deep_struct # rubocop: disable Metrics/MethodLength to_struct.tap do |struct| struct.to_h.each do |key, val| struct[key] = if val.is_a?(Array) val.map { |i| i.respond_to?(:to_deep_struct) ? i.to_deep_struct : i } elsif val.is_a?(Hash) val.to_deep_struct else val end end end end |
#to_dynamodb ⇒ Object
52 53 54 55 56 |
# File 'lib/yake/support/hash.rb', line 52 def to_dynamodb map do |key, val| { key => val.is_a?(Hash) ? { M: val.to_dynamodb } : val.to_dynamodb } end.reduce(&:merge) end |
#to_form ⇒ Object
16 |
# File 'lib/yake/support/hash.rb', line 16 def to_form = URI.encode_www_form(self) |
#to_h_from_dynamodb ⇒ Object
rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/yake/support/hash.rb', line 58 def to_h_from_dynamodb # rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength decode = lambda do |i| type, val = i.first case type.to_sym when :BOOL, :S then val when :NULL then nil when :NS then Set.new(val.map { |x| x =~ /^[0-9]+$/ ? x.to_i : x.to_f }) when :SS then Set.new(val) when :N then val =~ /^[0-9]+$/ ? val.to_i : val.to_f when :L then val.map(&decode) when :M then val.transform_values(&decode) end end map do |key, val| { key => decode.call(val) } end.reduce(&:merge) end |
#to_json_sorted ⇒ Object
17 |
# File 'lib/yake/support/hash.rb', line 17 def to_json_sorted = deep_sort.to_json |
#to_struct ⇒ Object
18 |
# File 'lib/yake/support/hash.rb', line 18 def to_struct = Struct.new(*keys.map(&:to_sym)).new(*values) |