Sha256: feb5fe3be52f631efc00243563a7abdf74a65372ab540c36e5d556130b9cd038
Contents?: true
Size: 1.32 KB
Versions: 35
Compression:
Stored size: 1.32 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 elsif defined?(BigDecimal) && value.is_a?(BigDecimal) value.to_f 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
35 entries across 35 versions & 1 rubygems