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

Version Path
ree_lib-1.0.35 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.34 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.33 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.32 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.31 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.30 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.29 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.28 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.27 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.26 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.24 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.23 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.22 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.21 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.20 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.19 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.18 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb
ree_lib-1.0.17 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/float.rb