Class: ShallowAttributes::Type::DateTime Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow_attributes/type/date_time.rb

Overview

This class is abstract.

Abstract class for typecast object to DateTime type.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#coerce(value, _options = {}) ⇒ DateTime

Convert value to DateTime type

Examples:

Convert integer to datetime value

ShallowAttributes::Type::DateTime.new.coerce('Thu Nov 29 14:33:20 GMT 2001')
  # => '2001-11-29T14:33:20+00:00'

Raises:

Since:

  • 0.1.0


25
26
27
28
29
30
31
32
33
34
# File 'lib/shallow_attributes/type/date_time.rb', line 25

def coerce(value, _options = {})
  case value
  when ::DateTime then value
  when ::Time then ::DateTime.parse(value.to_s)
  else
    ::DateTime.parse(value)
  end
rescue
  raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "DateTime")
end