class PureValidator::Validators::NumericalityValidator
Attributes
errors[RW]
object[RW]
options[RW]
Public Class Methods
new(object, options)
click to toggle source
# File lib/pure_validator/validators/numericality_validator.rb, line 19 def initialize(object, options) @object, @options = object, options @errors = [] end
validate(number, options)
click to toggle source
Validates that number
satisfies all validation rules defined in options
@param number [Numeric] number to validate @param options [Hash] validation rules @return [Array] empty array if number is valid, array of error messages otherwise
# File lib/pure_validator/validators/numericality_validator.rb, line 7 def self.validate(number, options) self.new(number, options).validate end
validate_options(options)
click to toggle source
# 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
Public Instance Methods
add_error!(key, number=nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
validate()
click to toggle source
# 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