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

Version Path
ree_lib-1.0.70 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.64 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb