Class: ActiveModel::Type::Integer

Inherits:
Value show all
Includes:
Helpers::Immutable, Helpers::Numeric
Defined in:
activemodel/lib/active_model/type/integer.rb

Overview

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

Constant Summary collapse

DEFAULT_LIMIT =

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

4

Instance Attribute Summary

Attributes inherited from Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from Helpers::Numeric

#cast, #changed?

Methods included from Helpers::Immutable

#mutable?

Methods inherited from Value

#==, #as_json, #assert_valid_value, #binary?, #cast, #changed?, #changed_in_place?, #force_equality?, #hash, #map, #mutable?, #serialized?, #type_cast_for_schema, #value_constructed_by_mass_assignment?

Methods included from SerializeCastValue

included, #itself_if_serialize_cast_value_compatible, serialize

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

#initializeInteger

Returns a new instance of Integer.



52
53
54
55
56
# File 'activemodel/lib/active_model/type/integer.rb', line 52

def initialize(**)
  super
  @max = max_value
  @min = min_value
end

Instance Method Details

#deserialize(value) ⇒ Object



62
63
64
65
# File 'activemodel/lib/active_model/type/integer.rb', line 62

def deserialize(value)
  return if value.blank?
  value.to_i
end

#serializable?(value) {|cast_value| ... } ⇒ Boolean

Yields:

  • (cast_value)

Returns:



96
97
98
99
100
101
# File 'activemodel/lib/active_model/type/integer.rb', line 96

def serializable?(value)
  cast_value = cast(value)
  return true unless out_of_range?(cast_value)
  yield cast_value if block_given?
  false
end

#serialize(value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'activemodel/lib/active_model/type/integer.rb', line 67

def serialize(value)
  case value
  when ::Integer
    # noop
  when ::String
    int = value.to_i
    if int.zero? && value != "0"
      return if non_numeric_string?(value)
    end
    value = int
  else
    value = super
  end

  if out_of_range?(value)
    raise ActiveModel::RangeError, "#{value} is out of range for #{self.class} with limit #{_limit} bytes"
  end

  value
end

#serialize_cast_value(value) ⇒ Object

:nodoc:



88
89
90
91
92
93
94
# File 'activemodel/lib/active_model/type/integer.rb', line 88

def serialize_cast_value(value) # :nodoc:
  if out_of_range?(value)
    raise ActiveModel::RangeError, "#{value} is out of range for #{self.class} with limit #{_limit} bytes"
  end

  value
end

#typeObject



58
59
60
# File 'activemodel/lib/active_model/type/integer.rb', line 58

def type
  :integer
end