Sha256: fc9fc91898e2b9ae3ed066d516b594002afbf0cd82a7860d2e702c84c0b2b0e1

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

class ReeMapper::Integer < ReeMapper::AbstractType
  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
  def serialize(value, name:, role: nil)
    if value.is_a? Integer
      value
    else
      raise ReeMapper::TypeError, "`#{name}` should be an integer"
    end
  end

  contract(Any , Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]]=> Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
  def cast(value, name:, role: nil)
    if value.is_a?(Integer)
      value
    elsif value.is_a?(String)
      value = Integer(value, exception: false)
      if value.nil?
        raise ReeMapper::CoercionError, "`#{name}` is invalid integer"
      end
      value
    else
      raise ReeMapper::TypeError, "`#{name}` should be an integer"
    end
  end

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::TypeError)
  def db_dump(value, name:, role: nil)
    serialize(value, name: name, role: role)
  end

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
  def db_load(value, name:, role: nil)
    cast(value, name: name, role: role)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ree_lib-1.0.85 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb