Class: StoreSchema::Converter::DateTime

Inherits:
Base
  • Object
show all
Defined in:
lib/store_schema/converter/date_time.rb

Constant Summary collapse

DATETIME_DB_FORMAT =

Returns the database format for storing a DateTime object.

Returns:

  • (String)

    the database format for storing a DateTime object.

"%Y-%m-%d %H:%M:%S.%N"

Instance Attribute Summary

Attributes inherited from Base

#value

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from StoreSchema::Converter::Base

Instance Method Details

#from_dbDateTime

Converts the Base#value to a Ruby-type value.

Returns:


32
33
34
# File 'lib/store_schema/converter/date_time.rb', line 32

def from_db
  ::DateTime.parse(value)
end

#to_dbString, false

Converts the Base#value to a database-storable value.

Returns:


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/store_schema/converter/date_time.rb', line 13

def to_db
  case value
  when ::DateTime, ::Date
    value.strftime(DATETIME_DB_FORMAT)
  when ::Time
    value.utc.strftime(DATETIME_DB_FORMAT)
  when ::String
    ::DateTime.parse(value).strftime(DATETIME_DB_FORMAT)
  else
    false
  end
rescue
  false
end