Sha256: 2e97f6645bb104e5da261faf1a6a25d0ee2044ee56dca9a9aca4013b38db1aa6

Contents?: true

Size: 1.03 KB

Versions: 33

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

class ReeMapper::Integer < ReeMapper::AbstractType
  contract(Any => Integer).throws(ReeMapper::TypeError)
  def serialize(value)
    if value.is_a? Integer
      value
    else
      raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
    end
  end

  contract(Any => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
  def cast(value)
    if value.is_a?(Integer)
      value
    elsif value.is_a?(String)
      coerced_value = Integer(value, exception: false)
      if coerced_value.nil?
        raise ReeMapper::CoercionError.new("is invalid integer, got `#{truncate(value.inspect)}`")
      end
      coerced_value
    else
      raise ReeMapper::TypeError.new("should be an integer, got `#{truncate(value.inspect)}`")
    end
  end

  contract(Any => Integer).throws(ReeMapper::TypeError)
  def db_dump(value)
    serialize(value)
  end

  contract(Any => Integer).throws(ReeMapper::CoercionError, ReeMapper::TypeError)
  def db_load(value)
    cast(value)
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
ree_lib-1.1.1 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.1.0 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.124 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb