Class: AttrTypecastable::Types::DateTime

Inherits:
Base
  • Object
show all
Defined in:
lib/attr_typecastable/types/date_time.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #typecast

Constructor Details

This class inherits a constructor from AttrTypecastable::Types::Base

Instance Method Details

#do_typecast(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/attr_typecastable/types/date_time.rb', line 10

def do_typecast(value)
  return value if value.is_a?(::DateTime)

  case value
  when ::String
    value.to_time.to_datetime
  when ::Time, ::Date
    value.to_datetime
  else
    raise CastError, "cannot convert to DateTime"
  end
end