Class: Hash
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(&block) ⇒ Object
- #deep_transform_keys!(&block) ⇒ Object
- #encode64 ⇒ Object
- #except(*keys) ⇒ Object
- #strict_encode64 ⇒ Object
- #stringify_names ⇒ Object
- #stringify_names! ⇒ Object
- #symbolize_names ⇒ Object
- #symbolize_names! ⇒ Object
- #to_deep_struct ⇒ Object
- #to_dynamodb ⇒ Object
- #to_form ⇒ Object
- #to_h_from_dynamodb ⇒ Object
- #to_json_sorted ⇒ Object
- #to_struct ⇒ Object
Instance Method Details
#deep_keys ⇒ Object
13 |
# File 'lib/yake/support.rb', line 13 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
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/yake/support.rb', line 31 def deep_merge(other, &block) 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
14 |
# File 'lib/yake/support.rb', line 14 def deep_sort = sort.map { |k,v| [ k, v.try(:deep_sort) { |x| x } ] }.to_h |
#deep_transform_keys(&block) ⇒ Object
15 |
# File 'lib/yake/support.rb', line 15 def deep_transform_keys(&block) = deep_transform(:transform_keys, &block) |
#deep_transform_keys!(&block) ⇒ Object
16 |
# File 'lib/yake/support.rb', line 16 def deep_transform_keys!(&block) = deep_transform(:transform_keys!, &block) |
#encode64 ⇒ Object
17 |
# File 'lib/yake/support.rb', line 17 def encode64 = to_json.encode64 |
#except(*keys) ⇒ Object
18 |
# File 'lib/yake/support.rb', line 18 def except(*keys) = reject { |key,_| keys.include? key } |
#strict_encode64 ⇒ Object
19 |
# File 'lib/yake/support.rb', line 19 def strict_encode64 = to_json.strict_encode64 |
#stringify_names ⇒ Object
20 |
# File 'lib/yake/support.rb', line 20 def stringify_names = deep_transform_keys(&:to_s) |
#stringify_names! ⇒ Object
21 |
# File 'lib/yake/support.rb', line 21 def stringify_names! = deep_transform_keys!(&:to_s) |
#symbolize_names ⇒ Object
22 |
# File 'lib/yake/support.rb', line 22 def symbolize_names = deep_transform_keys(&:to_sym) |
#symbolize_names! ⇒ Object
23 |
# File 'lib/yake/support.rb', line 23 def symbolize_names! = deep_transform_keys!(&:to_sym) |
#to_deep_struct ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/yake/support.rb', line 45 def to_deep_struct to_struct.tap do |struct| struct.to_h.each do |key, val| struct[key] = if val.is_a?(Array) val.map do |item| item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item end elsif val.is_a?(Hash) val.to_deep_struct else val end end end end |
#to_dynamodb ⇒ Object
61 62 63 64 65 |
# File 'lib/yake/support.rb', line 61 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
24 |
# File 'lib/yake/support.rb', line 24 def to_form = URI.encode_www_form(self) |
#to_h_from_dynamodb ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/yake/support.rb', line 67 def to_h_from_dynamodb decode = -> (i) do type, val = i.first case type.to_sym when :S then 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 === val } end.reduce(&:merge) end |
#to_json_sorted ⇒ Object
25 |
# File 'lib/yake/support.rb', line 25 def to_json_sorted = deep_sort.to_json |
#to_struct ⇒ Object
26 |
# File 'lib/yake/support.rb', line 26 def to_struct = Struct.new(*keys.map(&:to_sym)).new(*values) |