123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::Type::Integer

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
BigInteger, ActiveRecord::Type::DecimalWithoutScale, ActiveRecord::Type::UnsignedInteger, ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer, ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Oid
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Value
Instance Chain:
self, Value
Inherits: ActiveModel::Type::Value
Defined in: activemodel/lib/active_model/type/integer.rb

Overview

Active Model Integer Type

::ActiveModel::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)

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

::ActiveModel::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

Class Method Summary

Value - Inherited

.new

Initializes a type with three basic configuration settings: precision, limit, and scale.

Instance Attribute Summary

Value - Inherited

Instance Method Summary

Value - Inherited

#==, #as_json, #assert_valid_value,
#cast

::ActiveModel::Type casts a value from user input (e.g. from a setter).

#changed?

Determines whether a value has changed for dirty checking.

#changed_in_place?

Determines whether the mutable value has been modified since it was read.

#deserialize

Converts a value from database input to the appropriate ruby type.

#eql?

Alias for Value#==.

#hash,
#serializable?

Returns true if this type can convert value to a type that is usable by the database.

#serialize

Casts a value from the ruby type to a type that the database knows how to understand.

#type

Returns the unique type name as a ::Symbol.

#cast_value

Convenience method for types which do not need separate type casting behavior for user and database inputs.

Constructor Details

.newInteger

[ GitHub ]

  
# File 'activemodel/lib/active_model/type/integer.rb', line 51

def initialize(**)
  super
  @range = min_value...max_value
end

Instance Method Details

#deserialize(value)

[ GitHub ]

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

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

#serializable?(value) ⇒ Boolean

[ GitHub ]

  
# File 'activemodel/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)

[ GitHub ]

  
# File 'activemodel/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

[ GitHub ]

  
# File 'activemodel/lib/active_model/type/integer.rb', line 56

def type
  :integer
end