123456789_123456789_123456789_123456789_123456789_

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

Comparability - Included

COMPARE_CHECKS

Class Method Summary

::ActiveModel::EachValidator - Inherited

.new

Returns a new validator instance.

::ActiveModel::Validator - Inherited

.kind

Returns the kind of the validator.

.new

Accepts options that will be made available through the options reader.

Instance Attribute Summary

Instance Method Summary

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

#kind

Returns the kind for this validator.

#validate

Override this method in subclasses with validation logic, adding errors to the records #errors array where necessary.

Constructor Details

This class inherits a constructor from ActiveModel::EachValidator

Instance Method Details

#allow_only_integer?(record) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/numericality.rb', line 118

def allow_only_integer?(record)
  resolve_value(record, options[:only_integer])
end

#check_validity!

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/numericality.rb', line 22

def check_validity!
  options.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

  options.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 filtered_options(value)
  filtered = options.except(*RESERVED_OPTIONS)
  filtered[:value] = value
  filtered
end

#is_hexadecimal_literal?(raw_value) ⇒ Boolean (private)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/numericality.rb', line 94

def is_number?(raw_value, precision, scale)
  if options[: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)

[ GitHub ]

  
# 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, **filtered_options(value))
    return
  end

  if allow_only_integer?(record) && !is_integer?(value)
    record.errors.add(attr_name, :not_an_integer, **filtered_options(value))
    return
  end

  value = parse_as_number(value, precision, scale)

  options.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, **filtered_options(value))
      end
    elsif RANGE_CHECKS.include?(option)
      unless value.public_send(RANGE_CHECKS[option], option_value)
        record.errors.add(attr_name, option, **filtered_options(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, **filtered_options(value).merge!(count: option_value))
      end
    end
  end
end