Class: Hash

Inherits:
Object show all
Defined in:
lib/yake/support/hash.rb

Overview

Hash helpers

Instance Method Summary collapse

Instance Method Details

#deep_keysObject



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



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_sortObject



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_keysObject



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!, &)

#encode64Object



10
# File 'lib/yake/support/hash.rb', line 10

def encode64                = to_json.encode64

#strict_encode64Object



11
# File 'lib/yake/support/hash.rb', line 11

def strict_encode64         = to_json.strict_encode64

#stringify_namesObject



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_namesObject



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_structObject

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_dynamodbObject



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_formObject



16
# File 'lib/yake/support/hash.rb', line 16

def to_form                 = URI.encode_www_form(self)

#to_h_from_dynamodbObject

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_sortedObject



17
# File 'lib/yake/support/hash.rb', line 17

def to_json_sorted          = deep_sort.to_json

#to_structObject



18
# File 'lib/yake/support/hash.rb', line 18

def to_struct               = Struct.new(*keys.map(&:to_sym)).new(*values)