Class: ActiveModel::Validations::NumericalityValidator
Do not use. This class is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
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 20/\A[+-]?0[xX]/
-
INTEGER_REGEX =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 18/\A[-]?\d\z/
-
NUMBER_CHECKS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 14{ odd: :odd?, even: :even? }
-
RANGE_CHECKS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 13{ in: :in? }
-
RESERVED_OPTIONS =
# File 'activemodel/lib/active_model/validations/numericality.rb', line 16COMPARE_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, precision: Float::DIG, scale: nil)
- #allow_only_integer?(record) ⇒ Boolean private
- #filtered_options(value) private
- #is_hexadecimal_literal?(raw_value) ⇒ Boolean private
- #is_integer?(raw_value) ⇒ Boolean private
- #is_number?(raw_value, precision, scale) ⇒ Boolean private
- #option_as_number(record, option_value, precision, scale) private
- #parse_as_number(raw_value, precision, scale) private
- #parse_float(raw_value, precision, scale) private
- #prepare_value_for_validation(value, record, attr_name) private
- #record_attribute_changed_in_place?(record, attr_name) ⇒ Boolean private
- #round(raw_value, scale) 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. |
#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 118
def allow_only_integer?(record) resolve_value(record, [:only_integer]) end
#check_validity!
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 22
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) raise ArgumentError, ":#{option} must be a range" end end end
#filtered_options(value) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 112
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 108
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 104
def is_integer?(raw_value) INTEGER_REGEX.match?(raw_value.to_s) end
#is_number?(raw_value, precision, scale) ⇒ Boolean
(private)
# File 'activemodel/lib/active_model/validations/numericality.rb', line 94
def is_number?(raw_value, precision, scale) if [:only_numeric] && !raw_value.is_a?(Numeric) return false end !parse_as_number(raw_value, precision, scale).nil? rescue ArgumentError, TypeError false end
#option_as_number(record, option_value, precision, scale) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 68
def option_as_number(record, option_value, precision, scale) parse_as_number(resolve_value(record, option_value), precision, scale) end
#parse_as_number(raw_value, precision, scale) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 72
def parse_as_number(raw_value, precision, scale) if raw_value.is_a?(Float) parse_float(raw_value, precision, scale) elsif raw_value.is_a?(BigDecimal) round(raw_value, scale) elsif raw_value.is_a?(Numeric) raw_value elsif is_integer?(raw_value) raw_value.to_i elsif !is_hexadecimal_literal?(raw_value) parse_float(Kernel.Float(raw_value), precision, scale) end end
#parse_float(raw_value, precision, scale) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 86
def parse_float(raw_value, precision, scale) round(raw_value, scale).to_d(precision) end
#prepare_value_for_validation(value, record, attr_name) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 122
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 143
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
#round(raw_value, scale) (private)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 90
def round(raw_value, scale) scale ? raw_value.round(scale) : raw_value end
#validate_each(record, attr_name, value, precision: Float::DIG, scale: nil)
[ GitHub ]# File 'activemodel/lib/active_model/validations/numericality.rb', line 36
def validate_each(record, attr_name, value, precision: Float::DIG, scale: nil) unless is_number?(value, precision, scale) 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, precision, scale) .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) 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 = option_as_number(record, option_value, precision, scale) unless value.public_send(COMPARE_CHECKS[option], option_value) record.errors.add(attr_name, option, ** (value).merge!(count: option_value)) end end end end