Class: Aws::Record::Marshalers::TimeMarshaler

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TimeMarshaler

Returns a new instance of TimeMarshaler.



9
10
11
12
# File 'lib/aws-record/record/marshalers/time_marshaler.rb', line 9

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

Instance Method Details

#serialize(raw_value) ⇒ Object



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

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

#type_cast(raw_value) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/aws-record/record/marshalers/time_marshaler.rb', line 14

def type_cast(raw_value)
  value = _format(raw_value)
  if !@use_local_time && value.is_a?(::Time)
    value.utc
  else
    value
  end
end