Class: PureValidator::Validators::NumericalityValidator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options) ⇒ NumericalityValidator

Returns a new instance of NumericalityValidator.



19
20
21
22
# File 'lib/pure_validator/validators/numericality_validator.rb', line 19

def initialize(object, options)
  @object, @options = object, options
  @errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18

def errors
  @errors
end

#objectObject

Returns the value of attribute object.



18
19
20
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18

def object
  @object
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18

def options
  @options
end

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
# File 'lib/pure_validator/validators/numericality_validator.rb', line 7

def self.validate(number, options)
  self.new(number, options).validate
end

.validate_options(options) ⇒ Object



11
12
13
14
15
16
# File 'lib/pure_validator/validators/numericality_validator.rb', line 11

def self.validate_options(options)
  PureValidator::ArgsValidator.is_hash!(options, :validation_rule)
  PureValidator::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

Instance Method Details

#add_error!(key, number = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/pure_validator/validators/numericality_validator.rb', line 51

def add_error!(key, number=nil)
  if number
    errors << PureValidator::I18n.t(key, number: number)
  else
    errors << PureValidator::I18n.t(key)
  end
end

#handle_compare(key, condition, error_key) ⇒ Object



37
38
39
40
41
42
# File 'lib/pure_validator/validators/numericality_validator.rb', line 37

def handle_compare(key, condition, error_key)
  return unless options[key]
  if object.send(condition, options[key])
    add_error!(error_key, options[key])
  end
end

#handle_condition(key, condition, error_key) ⇒ Object



44
45
46
47
48
49
# File 'lib/pure_validator/validators/numericality_validator.rb', line 44

def handle_condition(key, condition, error_key)
  return unless options[key]
  unless object.send(condition)
    add_error!(error_key)
  end
end

#validateObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pure_validator/validators/numericality_validator.rb', line 24

def validate
  return errors if object.nil?

  handle_compare(:greater_than, :<=, 'errors.should_be_greater_than')
  handle_compare(:greater_than_or_equal_to, :<, 'errors.should_be_greater_than_or_equal_to')
  handle_compare(:less_than, :>=, 'errors.should_be_less_than')
  handle_compare(:less_than_or_equal_to, :>, 'errors.should_be_less_than_or_equal_to')
  handle_condition(:even, :even?, 'errors.should_be_even')
  handle_condition(:odd, :odd?, 'errors.should_be_odd')

  errors
end