Class: AWS::Record::Attributes::DateTimeAttr
- Defined in:
- lib/aws/record/attributes.rb
Instance Attribute Summary
Attributes inherited from BaseAttr
Class Method Summary collapse
- .allow_set? ⇒ Boolean
-
.serialize(datetime, options = {}) ⇒ String
Returns a DateTime object encoded as a string (suitable for sorting).
-
.type_cast(raw_value, options = {}) ⇒ DateTime?
Returns value cast to a DateTime object.
Methods inherited from BaseAttr
#default_value, deserialize, #deserialize, #initialize, #persist_as, #serialize, #set?, #type_cast
Constructor Details
This class inherits a constructor from AWS::Record::Attributes::BaseAttr
Class Method Details
.allow_set? ⇒ Boolean
377 378 379 |
# File 'lib/aws/record/attributes.rb', line 377 def self.allow_set? true end |
.serialize(datetime, options = {}) ⇒ String
Returns a DateTime object encoded as a string (suitable for sorting).
attribute.serialize(DateTime.parse('2001-01-01'))
#=> '2001-01-01T00:00:00:Z)
368 369 370 371 372 373 374 |
# File 'lib/aws/record/attributes.rb', line 368 def self.serialize datetime, = {} unless datetime.is_a?(DateTime) msg = "expected a DateTime value, got #{datetime.class}" raise ArgumentError, msg end datetime.strftime('%Y-%m-%dT%H:%M:%S%Z') end |
.type_cast(raw_value, options = {}) ⇒ DateTime?
Returns value cast to a DateTime object. Empty strings are cast to nil. Values are cast first to strings and then passed to DateTime.parse. Integers are treated as timestamps.
datetime_attribute.type_cast('2000-01-02')
#=> #<DateTime: 4903091/2,0,2299161>
datetime_attribute.type_cast(1306170146)
#<DateTime: 106086465073/43200,-7/24,2299161>
datetime_attribute.type_cast('')
#=> nil
datetime_attribute.type_cast(nil)
#=> nil
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/aws/record/attributes.rb', line 339 def self.type_cast raw_value, = {} case raw_value when nil then nil when '' then nil when DateTime then raw_value when Integer then begin DateTime.parse(Time.at(raw_value).to_s) # timestamp rescue nil end else begin DateTime.parse(raw_value.to_s) # Time, Date or String rescue nil end end end |