Class: ActiveModel::Type::Integer
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Value
|
|
|
Instance Chain:
|
|
| 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})
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
-
DEFAULT_LIMIT =
# File 'activemodel/lib/active_model/type/integer.rb', line 58
Column storage size in bytes. 4 bytes means an integer as opposed to smallint etc.
4
Helpers::Numeric - Included
Class Method Summary
- .new ⇒ Integer constructor
Value - Inherited
| .new | Initializes a type with three basic configuration settings: precision, limit, and scale. |
Instance Attribute Summary
Helpers::Immutable - Included
Value - Inherited
| #limit, #precision, #scale, | |
| #binary? | These predicates are not documented, as I need to look further into their use, and see if they can be removed entirely. |
| #mutable?, #serialized? | |
Instance Method Summary
- #deserialize(value)
- #init_from_schema_json(coder, references)
- #serializable?(value) {|cast_value| ... } ⇒ Boolean
- #serialize(value)
- #type
- #_limit private
- #cast_value(value) private
-
#max_cast_bytesize
private
Only the first bytes of a string are cast, to bound the work done by
to_ion very long input. - #max_value private
- #min_value private
- #out_of_range?(value) ⇒ Boolean private
- #serialize_cast_value(value) Internal use only
Helpers::Numeric - Included
| #cast, #changed?, #serialize, #serialize_cast_value, #equal_nan?, #non_numeric_string?, #number_to_non_number? |
Value - Inherited
| #==, #as_json, #as_schema_json, #assert_valid_value, | |
| #cast |
|
| #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, #init_from_schema_json, | |
| #serializable? | Returns true if this type can convert |
| #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 |
| #cast_value | Convenience method for types which do not need separate type casting behavior for user and database inputs. |
| #force_equality?, #map, | |
| #type_cast_for_schema |
|
| #value_constructed_by_mass_assignment? | |
SerializeCastValue - Included
Constructor Details
.new ⇒ Integer
Instance Method Details
#_limit (private)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 153
def _limit limit || DEFAULT_LIMIT end
#cast_value(value) (private)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 126
def cast_value(value) case value when ::Integer value when ::String max_bytesize = max_cast_bytesize str = value.bytesize > max_bytesize ? value.byteslice(0, max_bytesize) : value str.to_i rescue nil else value.to_i rescue nil end end
#deserialize(value)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 70
def deserialize(value) return if value.blank? value.to_i end
#init_from_schema_json(coder, references)
[ GitHub ]#max_cast_bytesize (private)
Only the first bytes of a string are cast, to bound the work done by
to_i on very long input.
# File 'activemodel/lib/active_model/type/integer.rb', line 149
def max_cast_bytesize _limit * 4 end
#max_value (private)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 139
def max_value 1 << (_limit * 8 - 1) # 8 bits per byte with one bit for sign end
#min_value (private)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 143
def min_value -max_value end
#out_of_range?(value) ⇒ Boolean (private)
#serializable?(value) {|cast_value| ... } ⇒ Boolean
# File 'activemodel/lib/active_model/type/integer.rb', line 104
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)
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 75
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)
# File 'activemodel/lib/active_model/type/integer.rb', line 96
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
#type
[ GitHub ]# File 'activemodel/lib/active_model/type/integer.rb', line 66
def type :integer end