Class: NotNaughty::NumericalityValidation
- Inherits:
-
FormatValidation
- Object
- Validation
- FormatValidation
- NotNaughty::NumericalityValidation
- Defined in:
- lib/not_naughty/validations/numericality_validation.rb
Overview
Validates numericality of obj’s attribute via an regular expression.
Unless the validation succeeds an error hash (:attribute => :message) is added to the obj’s instance of Errors.
Options:
:only_integer
-
validates with
/^[+-]?\d+$/
(false) :message
-
see NotNaughty::Errors for details
:if
-
see NotNaughty::Validation::Condition for details
:unless
-
see NotNaughty::Validation::Condition for details
Example:
obj = '-12.2' #
def obj.errors() @errors ||= NotNauthy::Errors.new end
NumericalityValidation.new({}, :to_s).call obj, :to_s, '-12.2'
obj.errors.on(:to_s).any? # => false
NumericalityValidation.new({:only_integer => true}, :to_s).
call obj, :to_s, '-12.2'
obj.errors.on(:to_s).any? # => true
Constant Summary
Constants inherited from FormatValidation
Constants inherited from Validation
Instance Attribute Summary
Attributes inherited from Validation
Instance Method Summary collapse
-
#initialize(valid, attributes) ⇒ NumericalityValidation
constructor
:nodoc:.
Methods inherited from Validation
#call_with_conditions, #call_without_conditions, inherited, load, load_paths, new
Constructor Details
#initialize(valid, attributes) ⇒ NumericalityValidation
:nodoc:
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/not_naughty/validations/numericality_validation.rb', line 30 def initialize(valid, attributes) #:nodoc: valid[:with] = if valid[:only_integer] valid[:message] ||= '%s is not an integer.' /^[+-]?\d+$/ else valid[:message] ||= '%s is not a number.' /^[+-]?\d*\.?\d+$/ end super valid, attributes end |