Module: Dynamoid::TypeCasting

Defined in:
lib/dynamoid/type_casting.rb

Defined Under Namespace

Classes: ArrayTypeCaster, Base, BinaryTypeCaster, BooleanTypeCaster, CustomTypeCaster, DateTimeTypeCaster, DateTypeCaster, IntegerTypeCaster, MapTypeCaster, NumberTypeCaster, RawTypeCaster, SerializedTypeCaster, SetTypeCaster, StringTypeCaster

Class Method Summary collapse

Class Method Details

.cast_attributes(attributes, attributes_options) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dynamoid/type_casting.rb', line 6

def self.cast_attributes(attributes, attributes_options)
  {}.tap do |h|
    attributes.symbolize_keys.each do |attribute, value|
      h[attribute] = cast_field(value, attributes_options[attribute])
    end
  end
end

.cast_field(value, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dynamoid/type_casting.rb', line 14

def self.cast_field(value, options)
  return value if options.nil?
  return nil if value.nil?

  type_caster = find_type_caster(options)
  if type_caster.nil?
    raise ArgumentError, "Unknown type #{options[:type]}"
  end

  type_caster.process(value)
end

.find_type_caster(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamoid/type_casting.rb', line 26

def self.find_type_caster(options)
  type_caster_class = case options[:type]
                      when :string     then StringTypeCaster
                      when :integer    then IntegerTypeCaster
                      when :number     then NumberTypeCaster
                      when :set        then SetTypeCaster
                      when :array      then ArrayTypeCaster
                      when :map        then MapTypeCaster
                      when :datetime   then DateTimeTypeCaster
                      when :date       then DateTypeCaster
                      when :raw        then RawTypeCaster
                      when :serialized then SerializedTypeCaster
                      when :boolean    then BooleanTypeCaster
                      when :binary     then BinaryTypeCaster
                      when Class       then CustomTypeCaster
                      end

  if type_caster_class.present?
    type_caster_class.new(options)
  end
end