Class: Mara::AttributeFormatter
- Inherits:
-
Object
- Object
- Mara::AttributeFormatter
- Defined in:
- lib/mara/attribute_formatter.rb
Overview
Helper class that provides Attribute Formatting for DynamoDB values.
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.flatten(value) ⇒ Object
Convert a Aws::DynamoDB::Types::AttributeValue to a raw value.
-
.format(value) ⇒ Hash
Format a value into a DynamoDB valid format.
Class Method Details
.flatten(value) ⇒ Object
Convert a Aws::DynamoDB::Types::AttributeValue to a raw value
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mara/attribute_formatter.rb', line 65 def flatten(value) unless value.is_a?(Aws::DynamoDB::Types::AttributeValue) raise ArgumentError, 'Not an attribute type' end if value.s.present? value.s elsif value.n.present? format_number(value.n) elsif value.ss.present? Set.new(value.ss) elsif value.ns.present? Set.new(value.ns.map { |v| format_number(v) }) elsif value.m.present? flatten_hash(value.m) elsif value.l.present? flatten_array(value.l) elsif value.null Mara::NULL elsif !value.bool.nil? value.bool else raise Error, 'Unexpected value type from DynamoDB' end end |
.format(value) ⇒ Hash
Format a value into a DynamoDB valid format.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mara/attribute_formatter.rb', line 30 def format(value) case value when true, false { bool: value } when nil, Mara::NullValue { null: true } when String { s: value } when Symbol { s: value.to_s } when Numeric { n: value.to_s } when Time { n: value.utc.to_i.to_s } when DateTime, Date format(value.to_time) when Hash format_hash(value) when Array format_array(value) when Set format_set(value) else raise Error, "Unexpected value type #{value.class.name} <#{value}>" end end |