Module: ActiveModel::Type::Helpers::Numeric
Do not use. This module is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
::ActiveModel::Type::BigInteger ,
::ActiveModel::Type::Decimal ,
::ActiveModel::Type::Float ,
::ActiveModel::Type::Integer ,
::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal ,
::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Money ,
::ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Oid ,
::ActiveRecord::ConnectionAdapters::SQLite3Adapter::SQLite3Integer ,
::ActiveRecord::Type::DecimalWithoutScale ,
::ActiveRecord::Type::UnsignedInteger
| |
Defined in: | activemodel/lib/active_model/type/helpers/numeric.rb |
Constant Summary
-
NUMERIC_REGEX =
private
# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 56/\A\s*[+-]?\d/
Instance Method Summary
- #cast(value)
- #changed?(old_value, _new_value, new_value_before_type_cast) ⇒ Boolean
- #serialize(value)
- #serialize_cast_value(value)
- #equal_nan?(old_value, new_value) ⇒ Boolean private
- #non_numeric_string?(value) ⇒ Boolean private
- #number_to_non_number?(old_value, new_value_before_type_cast) ⇒ Boolean private
Instance Method Details
#cast(value)
[ GitHub ]# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 15
def cast(value) # Checks whether the value is numeric. Spaceship operator # will return nil if value is not numeric. value = if value <=> 0 value else case value when true then 1 when false then 0 else value.presence end end super(value) end
#changed?(old_value, _new_value, new_value_before_type_cast) ⇒ Boolean
# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 31
def changed?(old_value, _new_value, new_value_before_type_cast) # :nodoc: (super || number_to_non_number?(old_value, new_value_before_type_cast)) && !equal_nan?(old_value, new_value_before_type_cast) end
#equal_nan?(old_value, new_value) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 37
def equal_nan?(old_value, new_value) (old_value.is_a?(::Float) || old_value.is_a?(BigDecimal)) && old_value.nan? && old_value.instance_of?(new_value.class) && new_value.nan? end
#non_numeric_string?(value) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 49
def non_numeric_string?(value) # 'wibble'.to_i will give zero, we want to make sure # that we aren't marking int zero to string zero as # changed. !NUMERIC_REGEX.match?(value) end
#number_to_non_number?(old_value, new_value_before_type_cast) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 44
def number_to_non_number?(old_value, new_value_before_type_cast) old_value != nil && !new_value_before_type_cast.is_a?(::Numeric) && non_numeric_string?(new_value_before_type_cast.to_s) end
#serialize(value)
[ GitHub ]# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 7
def serialize(value) cast(value) end
#serialize_cast_value(value)
[ GitHub ]# File 'activemodel/lib/active_model/type/helpers/numeric.rb', line 11
def serialize_cast_value(value) value end