Sha256: 1790c4831c1e75b4189ec72b7e13e49997b3ef69ce3058e25de0cc137a767c90
Contents?: true
Size: 1.25 KB
Versions: 18
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true class ReeMapper::Float < ReeMapper::AbstractType contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::TypeError) def serialize(value, name:, role: nil) if value.is_a?(Float) value else raise ReeMapper::TypeError, "`#{name}` should be a float" end end contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError) def cast(value, name:, role: nil) if value.is_a?(Float) value elsif value.is_a?(String) begin Float(value) rescue ArgumentError => e raise ReeMapper::CoercionError, "`#{name}` is invalid float" end else raise ReeMapper::TypeError, "`#{name}` should be a float" end end contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError) def db_dump(value, name:, role: nil) serialize(value, name: name, role: role) end contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Float).throws(ReeMapper::CoercionError, ReeMapper::TypeError) def db_load(value, name:, role: nil) cast(value, name: name, role: role) end end
Version data entries
18 entries across 18 versions & 1 rubygems