Class: AWS::Record::Attributes::DateAttr

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

.allow_set?Boolean

Returns:

  • (Boolean)

312
313
314
# File 'lib/aws/record/attributes.rb', line 312

def self.allow_set?
  true
end

.serialize(date, options = {}) ⇒ String

Returns a Date object encoded as a string (suitable for sorting).

attribute.serialize(DateTime.parse('2001-01-01'))
#=> '2001-01-01'

Parameters:

  • date (Date)

    The date to serialize.

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

Returns:

  • (String)

    Returns the date object serialized to a string ('YYYY-MM-DD').


304
305
306
307
308
309
# File 'lib/aws/record/attributes.rb', line 304

def self.serialize date, options = {}
  unless date.is_a?(Date)
    raise ArgumentError, "expected a Date value, got #{date.class}"
  end
  date.strftime('%Y-%m-%d')
end

.type_cast(raw_value, options = {}) ⇒ Date?

Returns value cast to a Date object. Empty strings are cast to nil. Values are cast first to strings and then passed to Date.parse. Integers are treated as timestamps.

date_attribute.type_cast('2000-01-02T10:11:12Z')
#=> #<Date: 4903091/2,0,2299161>

date_attribute.type_cast(1306170146)
#<Date: 4911409/2,0,2299161>

date_attribute.type_cast('')
#=> nil

date_attribute.type_cast(nil)
#=> nil

Parameters:

  • raw_value (Mixed)

    The value to cast to a Date object.

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

Returns:

  • (Date, nil)

272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/aws/record/attributes.rb', line 272

def self.type_cast raw_value, options = {}
  case raw_value
  when nil      then nil
  when ''       then nil
  when Date     then raw_value
  when Integer  then 
    begin
      Date.parse(Time.at(raw_value).to_s) # assumed timestamp
    rescue
      nil
    end
  else 
    begin
      Date.parse(raw_value.to_s) # Time, DateTime or String
    rescue
      nil
    end
  end
end