Class: AttrValidator::Validators::NumericalityValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_validator/validators/numericality_validator.rb

Class Method Summary collapse

Class Method Details

.validate(number, options) ⇒ Array

Validates that number satisfies all validation rules defined in options

Parameters:

  • number (Numeric)

    number to validate

  • options (Hash)

    validation rules

Returns:

  • (Array)

    empty array if number is valid, array of error messages otherwise



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/attr_validator/validators/numericality_validator.rb', line 7

def self.validate(number, options)
  return [] if number.nil?

  errors = []
  if options[:greater_than]
    errors << AttrValidator::I18n.t('errors.should_be_greater_than', number: options[:greater_than]) if number <= options[:greater_than]
  end
  if options[:greater_than_or_equal_to]
    errors << AttrValidator::I18n.t('errors.should_be_greater_than_or_equal_to', number: options[:greater_than_or_equal_to]) if number < options[:greater_than_or_equal_to]
  end
  if options[:less_than]
    errors << AttrValidator::I18n.t('errors.should_be_less_than', number: options[:less_than]) if number >= options[:less_than]
  end
  if options[:less_than_or_equal_to]
    errors << AttrValidator::I18n.t('errors.should_be_less_than_or_equal_to', number: options[:less_than_or_equal_to]) if number > options[:less_than_or_equal_to]
  end
  if options[:even]
    errors << AttrValidator::I18n.t('errors.should_be_even') unless number.even?
  end
  if options[:odd]
    errors << AttrValidator::I18n.t('errors.should_be_odd') unless number.odd?
  end
  errors
end

.validate_options(options) ⇒ Object



32
33
34
35
36
37
# File 'lib/attr_validator/validators/numericality_validator.rb', line 32

def self.validate_options(options)
  AttrValidator::ArgsValidator.is_hash!(options, :validation_rule)
  AttrValidator::ArgsValidator.has_only_allowed_keys!(options, [
    :greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to, :even, :odd
  ], :validation_rule)
end