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 Validation
Validation::BASEDIR, Validation::PATTERN
Instance Attribute Summary
Attributes inherited from Validation
Instance Method Summary collapse
-
#initialize(opts, attributes) ⇒ NumericalityValidation
constructor
:nodoc:.
Methods inherited from Validation
#call_with_conditions, #call_without_conditions, inherited, load, new
Constructor Details
#initialize(opts, 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(opts, attributes) #:nodoc: opts[:with] = if opts[:only_integer] opts[:message] ||= '#{"%s".humanize} is not an integer.' /^[+-]?\d+$/ else opts[:message] ||= '#{"%s".humanize} is not a number.' /^[+-]?\d*\.?\d+$/ end super opts, attributes end |