Class: AWS::Record::Attributes::DateTimeAttr

Inherits:
BaseAttr
  • Object
show all
Defined in:
lib/aws/record/attributes.rb

Instance Attribute Summary

Attributes inherited from BaseAttr

#name, #options

Class Method Summary collapse

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

.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)

Parameters:

  • datetime (DateTime)

    The datetime object to serialize.

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    Returns the datetime object serialized to a string in ISO8601 format (e.g. ā€˜2011-01-02T10:11:12Zā€™)



368
369
370
371
372
373
374
# File 'lib/aws/record/attributes.rb', line 368

def self.serialize datetime, options = {}
  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

Parameters:

  • raw_value (Mixed)

    The value to cast to a DateTime object.

  • options (Hash) (defaults to: {})

Returns:

  • (DateTime, 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, options = {}
  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