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