Class: AWS::Record::Attributes::DateAttr
- Defined in:
- lib/aws/record/attributes.rb
Instance Attribute Summary
Attributes inherited from BaseAttr
Class Method Summary collapse
- .allow_set? ⇒ Boolean
-
.serialize(date, options = {}) ⇒ String
Returns a Date object encoded as a string (suitable for sorting).
-
.type_cast(raw_value, options = {}) ⇒ Date?
Returns value cast to a Date 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
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'
304 305 306 307 308 309 |
# File 'lib/aws/record/attributes.rb', line 304 def self.serialize date, = {} 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
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, = {} 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 |