Sha256: db4384d0039dcce61cdc58e388ec817b61f6c100c833020fda3f617f84dbbff5
Contents?: true
Size: 1.2 KB
Versions: 17
Compression:
Stored size: 1.2 KB
Contents
# frozen_string_literal: true require 'date' class ReeMapper::Date < ReeMapper::AbstractType contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError) def serialize(value, role: nil) if value.class == Date value else raise ReeMapper::TypeError, "should be a date" end end contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError) def cast(value, role: nil) if value.class == Date value elsif value.class == DateTime || value.class == Time value.to_date elsif value.is_a?(String) begin Date.parse(value) rescue ArgumentError => e raise ReeMapper::CoercionError, "is invalid date" end else raise ReeMapper::TypeError, "should be a date" end end contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError) def db_dump(value, role: nil) serialize(value, role: role) end contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Date).throws(ReeMapper::TypeError, ReeMapper::CoercionError) def db_load(value, role: nil) cast(value, role: role) end end
Version data entries
17 entries across 17 versions & 1 rubygems