Class: Aws::Record::Marshalers::DateMarshaler

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DateMarshaler

Returns a new instance of DateMarshaler



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

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

Instance Method Details

#serialize(raw_value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/aws-record/record/marshalers/date_marshaler.rb', line 40

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

#type_cast(raw_value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aws-record/record/marshalers/date_marshaler.rb', line 25

def type_cast(raw_value)
  case raw_value
  when nil
    nil
  when ''
    nil
  when Date
    raw_value
  when Integer
    Date.parse(Time.at(raw_value).to_s) # assumed timestamp
  else
    Date.parse(raw_value.to_s) # Time, DateTime or String
  end
end