Class: Aws::Record::Marshalers::DateTimeMarshaler

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-record/record/marshalers/date_time_marshaler.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DateTimeMarshaler

Returns a new instance of DateTimeMarshaler



21
22
23
24
# File 'lib/aws-record/record/marshalers/date_time_marshaler.rb', line 21

def initialize(opts = {})
  @formatter = opts[:formatter] || Iso8601Formatter
  @use_local_time = opts[:use_local_time] ? true : false
end

Instance Method Details

#serialize(raw_value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aws-record/record/marshalers/date_time_marshaler.rb', line 35

def serialize(raw_value)
  datetime = type_cast(raw_value)
  if datetime.nil?
    nil
  elsif datetime.is_a?(::DateTime)
    @formatter.format(datetime)
  else
    msg = "expected a DateTime value or nil, got #{datetime.class}"
    raise ArgumentError, msg
  end
end

#type_cast(raw_value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/aws-record/record/marshalers/date_time_marshaler.rb', line 26

def type_cast(raw_value)
  value = _format(raw_value)
  if !@use_local_time && value.is_a?(::DateTime)
    value.new_offset(0)
  else
    value
  end
end