Class: Aws::Record::Marshalers::DateMarshaler
- Inherits:
-
Object
- Object
- Aws::Record::Marshalers::DateMarshaler
- Defined in:
- lib/aws-record/record/marshalers/date_marshaler.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ DateMarshaler
constructor
A new instance of DateMarshaler.
- #serialize(raw_value) ⇒ Object
- #type_cast(raw_value) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ DateMarshaler
Returns a new instance of DateMarshaler.
9 10 11 |
# File 'lib/aws-record/record/marshalers/date_marshaler.rb', line 9 def initialize(opts = {}) @formatter = opts[:formatter] || Iso8601Formatter end |
Instance Method Details
#serialize(raw_value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/aws-record/record/marshalers/date_marshaler.rb', line 26 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
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/aws-record/record/marshalers/date_marshaler.rb', line 13 def type_cast(raw_value) case raw_value when nil, '' 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 |