class ActiveModel::Type::Integer

Active Model Integer Type

Attribute type for integer representation. This type is registered under the :integer key.

class Person
  include ActiveModel::Attributes

  attribute :age, :integer
end

Values are cast using their to_i method, except for blank strings, which are cast to nil. If a to_i method is not defined or raises an error, the value will be cast to nil.

person = Person.new

person.age = "18"
person.age # => 18

person.age = ""
person.age # => nil

person.age = :not_an_integer
person.age # => nil (because Symbol does not define #to_i)

Serialization also works under the same principle. Non-numeric strings are serialized as nil, for example.

Serialization also validates that the integer can be stored using a limited number of bytes. If it cannot, an ActiveModel::RangeError will be raised. The default limit is 4 bytes, and can be customized when declaring an attribute:

class Person
  include ActiveModel::Attributes

  attribute :age, :integer, limit: 6
end

Constants

DEFAULT_LIMIT

Column storage size in bytes. 4 bytes means an integer as opposed to smallint etc.

Attributes

range[R]

Public Class Methods

new(**) click to toggle source
Calls superclass method
# File lib/active_model/type/integer.rb, line 51
def initialize(**)
  super
  @range = min_value...max_value
end

Public Instance Methods

deserialize(value) click to toggle source
# File lib/active_model/type/integer.rb, line 60
def deserialize(value)
  return if value.blank?
  value.to_i
end
serializable?(value) { |cast_value| ... } click to toggle source
# File lib/active_model/type/integer.rb, line 74
def serializable?(value)
  cast_value = cast(value)
  in_range?(cast_value) || begin
    yield cast_value if block_given?
    false
  end
end
serialize(value) click to toggle source
Calls superclass method
# File lib/active_model/type/integer.rb, line 65
def serialize(value)
  return if value.is_a?(::String) && non_numeric_string?(value)
  ensure_in_range(super)
end
type() click to toggle source
# File lib/active_model/type/integer.rb, line 56
def type
  :integer
end

Private Instance Methods

_limit() click to toggle source
# File lib/active_model/type/integer.rb, line 108
def _limit
  limit || DEFAULT_LIMIT
end
cast_value(value) click to toggle source
# File lib/active_model/type/integer.rb, line 89
def cast_value(value)
  value.to_i rescue nil
end
ensure_in_range(value) click to toggle source
# File lib/active_model/type/integer.rb, line 93
def ensure_in_range(value)
  unless in_range?(value)
    raise ActiveModel::RangeError, "#{value} is out of range for #{self.class} with limit #{_limit} bytes"
  end
  value
end
in_range?(value) click to toggle source
# File lib/active_model/type/integer.rb, line 85
def in_range?(value)
  !value || range.member?(value)
end
max_value() click to toggle source
# File lib/active_model/type/integer.rb, line 100
def max_value
  1 << (_limit * 8 - 1) # 8 bits per byte with one bit for sign
end
min_value() click to toggle source
# File lib/active_model/type/integer.rb, line 104
def min_value
  -max_value
end