Sha256: 9236c740794ba6e0f7b3f0b4b4ea382002aa17730d1c2d4f2c056550c087f64c

Contents?: true

Size: 1.08 KB

Versions: 31

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require 'date'

class ReeMapper::DateTime < ReeMapper::AbstractType
  contract(Any => DateTime).throws(ReeMapper::TypeError)
  def serialize(value)
    if value.class == DateTime
      value
    else
      raise ReeMapper::TypeError.new("should be a datetime, got `#{truncate(value.inspect)}`")
    end
  end

  contract(Any => DateTime).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
  def cast(value)
    if value.class == DateTime
      value
    elsif value.class == Time
      ReeDatetime::InDefaultTimeZone.new.call(value.to_datetime)
    elsif value.is_a?(String)
      begin
        ReeDatetime::InDefaultTimeZone.new.call(DateTime.parse(value))
      rescue ArgumentError
        raise ReeMapper::CoercionError.new("is invalid datetime, got `#{truncate(value.inspect)}`")
      end
    else
      raise ReeMapper::TypeError.new("should be a datetime, got `#{truncate(value.inspect)}`")
    end
  end

  contract(Any => DateTime)
  def db_dump(value)
    serialize(value)
  end

  contract(Any => DateTime)
  def db_load(value)
    cast(value)
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/date_time.rb