Class: ActiveModel::Validations::NumericalityValidator
Do not use. This class is for internal use only.
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
|
|
|
Instance Chain:
|
|
| Inherits: |
ActiveModel::EachValidator
|
| Defined in: | activemodel/lib/active_model/validations/numericality.rb |
Constant Summary
-
HEXADECIMAL_REGEX =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 21/\A[+-]?0[xX]/ -
INTEGER_REGEX =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 19/\A[-]?\d\z/ -
NUMBER_CHECKS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 15{ odd: :odd?, even: :even? }.freeze -
RANGE_CHECKS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 14{ in: :in? }.freeze -
RESERVED_OPTIONS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 17COMPARE_CHECKS.keys + NUMBER_CHECKS.keys + RANGE_CHECKS.keys + [:only_integer, :only_numeric]
Comparability - Included
Class Method Summary
::ActiveModel::EachValidator - Inherited
| .new | Returns a new validator instance. |
::ActiveModel::Validator - Inherited
Instance Attribute Summary
Instance Method Summary
- #check_validity!
- #validate_each(record, attr_name, value)
- #allow_only_integer?(record) ⇒ Boolean private
-
#cast_as_decimal(raw_value, decimal_type)
private
PostgreSQL's money casts only strings to its scale.
-
#cast_as_number(raw_value, decimal_type, cast)
private
The value is cast as the decimal attribute holds it, while an option value is taken as given.
- #decimal_type_for(record, attr_name) private
- #filtered_options(value) private
- #is_hexadecimal_literal?(raw_value) ⇒ Boolean private
- #is_integer?(raw_value) ⇒ Boolean private
- #is_number?(raw_value, decimal_type) ⇒ Boolean private
- #parse_as_number(raw_value, decimal_type, cast: true) private
- #prepare_value_for_validation(value, record, attr_name) private
- #record_attribute_changed_in_place?(record, attr_name) ⇒ Boolean private
ResolveValue - Included
Comparability - Included
::ActiveModel::EachValidator - Inherited
| #check_validity! | Hook method that gets called by the initializer allowing verification that the arguments supplied are valid. |
| #validate | Performs validation on the supplied record. |
| #validate_each | Override this method in subclasses with the validation logic, adding errors to the records #errors array where necessary. |
| #allow_blank?, #allow_nil?, #prepare_value_for_validation | |
::ActiveModel::Validator - Inherited
Constructor Details
This class inherits a constructor from ActiveModel::EachValidator
Instance Method Details
#allow_only_integer?(record) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 132
def allow_only_integer?(record) resolve_value(record, [:only_integer]) end
#cast_as_decimal(raw_value, decimal_type) (private)
PostgreSQL's money casts only strings to its scale.
# File 'activemodel/lib/active_model/validations/numericality.rb', line 103
def cast_as_decimal(raw_value, decimal_type) value = decimal_type.cast(raw_value) decimal_type.scale ? value.round(decimal_type.scale) : value end
#cast_as_number(raw_value, decimal_type, cast) (private)
The value is cast as the decimal attribute holds it, while an option
value is taken as given. Floats otherwise keep the Float::DIG guarantee.
# File 'activemodel/lib/active_model/validations/numericality.rb', line 92
def cast_as_number(raw_value, decimal_type, cast) if decimal_type cast ? cast_as_decimal(raw_value, decimal_type) : raw_value elsif raw_value.is_a?(Float) raw_value.to_d(Float::DIG) else raw_value end end
#check_validity!
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 23
def check_validity! .slice(*COMPARE_CHECKS.keys).each do |option, value| unless value.is_a?(Numeric) || value.is_a?(Proc) || value.is_a?(Symbol) raise ArgumentError, ":#{option} must be a number, a symbol or a proc" end end .slice(*RANGE_CHECKS.keys).each do |option, value| unless value.is_a?(Range) || value.is_a?(Proc) || value.is_a?(Symbol) raise ArgumentError, ":#{option} must be a range, a symbol or a proc" end end end
#decimal_type_for(record, attr_name) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 75
def decimal_type_for(record, attr_name) type = record.class.try(:type_for_attribute, attr_name) type if type&.type == :decimal || type&.scale end
#filtered_options(value) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 126
def (value) filtered = .except(*RESERVED_OPTIONS) filtered[:value] = value filtered end
#is_hexadecimal_literal?(raw_value) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 122
def is_hexadecimal_literal?(raw_value) HEXADECIMAL_REGEX.match?(raw_value.to_s) end
#is_integer?(raw_value) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 118
def is_integer?(raw_value) INTEGER_REGEX.match?(raw_value.to_s) end
#is_number?(raw_value, decimal_type) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 108
def is_number?(raw_value, decimal_type) if [:only_numeric] && !raw_value.is_a?(Numeric) return false end !parse_as_number(raw_value, decimal_type).nil? rescue ArgumentError, TypeError false end
#parse_as_number(raw_value, decimal_type, cast: true) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 80
def parse_as_number(raw_value, decimal_type, cast: true) if raw_value.is_a?(Numeric) cast_as_number(raw_value, decimal_type, cast) elsif is_integer?(raw_value) raw_value.to_i elsif !is_hexadecimal_literal?(raw_value) cast_as_number(Kernel.Float(raw_value), decimal_type, cast) end end
#prepare_value_for_validation(value, record, attr_name) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 136
def prepare_value_for_validation(value, record, attr_name) return value if record_attribute_changed_in_place?(record, attr_name) came_from_user = :"#{attr_name}_came_from_user?" if record.respond_to?(came_from_user) if record.public_send(came_from_user) raw_value = record.public_send(:"#{attr_name}_before_type_cast") elsif record.respond_to?(:read_attribute) raw_value = record.read_attribute(attr_name) end else before_type_cast = :"#{attr_name}_before_type_cast" if record.respond_to?(before_type_cast) raw_value = record.public_send(before_type_cast) end end raw_value || value end
#record_attribute_changed_in_place?(record, attr_name) ⇒ Boolean (private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 157
def record_attribute_changed_in_place?(record, attr_name) record.respond_to?(:attribute_changed_in_place?) && record.attribute_changed_in_place?(attr_name.to_s) end
#validate_each(record, attr_name, value)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 37
def validate_each(record, attr_name, value) decimal_type = decimal_type_for(record, attr_name) unless is_number?(value, decimal_type) record.errors.add(attr_name, :not_a_number, **(value)) return end if allow_only_integer?(record) && !is_integer?(value) record.errors.add(attr_name, :not_an_integer, **(value)) return end value = parse_as_number(value, decimal_type) .slice(*RESERVED_OPTIONS).each do |option, option_value| if NUMBER_CHECKS.include?(option) unless value.to_i.public_send(NUMBER_CHECKS[option]) record.errors.add(attr_name, option, **(value)) end elsif RANGE_CHECKS.include?(option) option_value = resolve_value(record, option_value) unless option_value.is_a?(Range) raise ArgumentError, ":#{option} must return a range" end unless value.public_send(RANGE_CHECKS[option], option_value) record.errors.add(attr_name, option, **(value).merge!(count: option_value)) end elsif COMPARE_CHECKS.include?(option) option_value = parse_as_number(resolve_value(record, option_value), decimal_type, cast: false) unless value.public_send(COMPARE_CHECKS[option], option_value) record.errors.add(attr_name, option, **(value).merge!(count: option_value)) end end end end