Class: ActiveModel::Type::Integer
- Includes:
 - Helpers::Numeric
 
- Defined in:
 - 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
  Direct Known Subclasses
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
Instance Method Summary collapse
- #deserialize(value) ⇒ Object
 - 
  
    
      #initialize  ⇒ Integer 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of Integer.
 - #serializable?(value) ⇒ Boolean
 - #serialize(value) ⇒ Object
 - 
  
    
      #serialize_cast_value(value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
:nodoc:.
 - #type ⇒ Object
 
Methods included from Helpers::Numeric
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
Constructor Details
#initialize ⇒ Integer
Returns a new instance of Integer.
      51 52 53 54  | 
    
      # File 'lib/active_model/type/integer.rb', line 51 def initialize(**) super @range = min_value...max_value end  | 
  
Instance Method Details
#deserialize(value) ⇒ Object
      60 61 62 63  | 
    
      # File 'lib/active_model/type/integer.rb', line 60 def deserialize(value) return if value.blank? value.to_i end  | 
  
#serializable?(value) ⇒ Boolean
      74 75 76 77 78 79 80  | 
    
      # 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) ⇒ Object
      65 66 67 68  | 
    
      # 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  | 
  
#serialize_cast_value(value) ⇒ Object
:nodoc:
      70 71 72  | 
    
      # File 'lib/active_model/type/integer.rb', line 70 def serialize_cast_value(value) # :nodoc: ensure_in_range(value) end  | 
  
#type ⇒ Object
      56 57 58  | 
    
      # File 'lib/active_model/type/integer.rb', line 56 def type :integer end  |