Class: PureValidator::Validators::NumericalityValidator
- Inherits:
-
Object
- Object
- PureValidator::Validators::NumericalityValidator
- Defined in:
- lib/pure_validator/validators/numericality_validator.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#object ⇒ Object
Returns the value of attribute object.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.validate(number, options) ⇒ Array
Validates that
number
satisfies all validation rules defined inoptions
. - .validate_options(options) ⇒ Object
Instance Method Summary collapse
- #add_error!(key, number = nil) ⇒ Object
- #handle_compare(key, condition, error_key) ⇒ Object
- #handle_condition(key, condition, error_key) ⇒ Object
-
#initialize(object, options) ⇒ NumericalityValidator
constructor
A new instance of NumericalityValidator.
- #validate ⇒ Object
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, ) @object, @options = object, @errors = [] end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
18 19 20 |
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18 def errors @errors end |
#object ⇒ Object
Returns the value of attribute object.
18 19 20 |
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18 def object @object end |
#options ⇒ Object
Returns the value of attribute options.
18 19 20 |
# File 'lib/pure_validator/validators/numericality_validator.rb', line 18 def @options end |
Class Method Details
.validate(number, options) ⇒ Array
Validates that number
satisfies all validation rules defined in options
7 8 9 |
# File 'lib/pure_validator/validators/numericality_validator.rb', line 7 def self.validate(number, ) self.new(number, ).validate end |
.validate_options(options) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/pure_validator/validators/numericality_validator.rb', line 11 def self.() PureValidator::ArgsValidator.is_hash!(, :validation_rule) PureValidator::ArgsValidator.has_only_allowed_keys!(, [ :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 [key] if object.send(condition, [key]) add_error!(error_key, [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 [key] unless object.send(condition) add_error!(error_key) end end |
#validate ⇒ Object
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 |