Class: AttrValidator::Validators::NumericalityValidator
- Inherits:
-
Object
- Object
- AttrValidator::Validators::NumericalityValidator
- Defined in:
- lib/attr_validator/validators/numericality_validator.rb
Class Method Summary collapse
-
.validate(number, options) ⇒ Array
Validates that
number
satisfies all validation rules defined inoptions
. - .validate_options(options) ⇒ Object
Class Method Details
.validate(number, options) ⇒ Array
Validates that number
satisfies all validation rules defined in options
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, ) return [] if number.nil? errors = [] if [:greater_than] errors << AttrValidator::I18n.t('errors.should_be_greater_than', number: [:greater_than]) if number <= [:greater_than] end if [:greater_than_or_equal_to] errors << AttrValidator::I18n.t('errors.should_be_greater_than_or_equal_to', number: [:greater_than_or_equal_to]) if number < [:greater_than_or_equal_to] end if [:less_than] errors << AttrValidator::I18n.t('errors.should_be_less_than', number: [:less_than]) if number >= [:less_than] end if [:less_than_or_equal_to] errors << AttrValidator::I18n.t('errors.should_be_less_than_or_equal_to', number: [:less_than_or_equal_to]) if number > [:less_than_or_equal_to] end if [:even] errors << AttrValidator::I18n.t('errors.should_be_even') unless number.even? end if [: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.() AttrValidator::ArgsValidator.is_hash!(, :validation_rule) AttrValidator::ArgsValidator.has_only_allowed_keys!(, [ :greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to, :even, :odd ], :validation_rule) end |