Module: Dynamoid::Undumping

Defined in:
lib/dynamoid/undumping.rb

Defined Under Namespace

Modules: UndumpHashHelper Classes: ArrayUndumper, Base, BinaryUndumper, BooleanUndumper, CustomTypeUndumper, DateTimeUndumper, DateUndumper, IntegerUndumper, MapUndumper, NumberUndumper, RawUndumper, SerializedUndumper, SetUndumper, StringUndumper

Class Method Summary collapse

Class Method Details

.find_undumper(options) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dynamoid/undumping.rb', line 29

def self.find_undumper(options)
  undumper_class = case options[:type]
                   when :string     then StringUndumper
                   when :integer    then IntegerUndumper
                   when :number     then NumberUndumper
                   when :set        then SetUndumper
                   when :array      then ArrayUndumper
                   when :map        then MapUndumper
                   when :datetime   then DateTimeUndumper
                   when :date       then DateUndumper
                   when :raw        then RawUndumper
                   when :serialized then SerializedUndumper
                   when :boolean    then BooleanUndumper
                   when :binary     then BinaryUndumper
                   when Class       then CustomTypeUndumper
                   end

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

.undump_attributes(attributes, attributes_options) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'lib/dynamoid/undumping.rb', line 6

def self.undump_attributes(attributes, attributes_options)
  {}.tap do |h|
    # ignore existing attributes not declared in document class
    attributes.symbolize_keys
      .select { |attribute| attributes_options.key?(attribute) }
      .each do |attribute, value|
      h[attribute] = undump_field(value, attributes_options[attribute])
    end
  end
end

.undump_field(value, options) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dynamoid/undumping.rb', line 17

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

  undumper = find_undumper(options)

  if undumper.nil?
    raise ArgumentError, "Unknown type #{options[:type]}"
  end

  undumper.process(value)
end