Class: Validatable::ValidatesNumericalityOf
- Inherits:
-
ValidationBase
- Object
- ValidationBase
- Validatable::ValidatesNumericalityOf
- Defined in:
- lib/validatable/validations/validates_numericality_of.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#only_integer ⇒ Object
Returns the value of attribute only_integer.
Attributes inherited from ValidationBase
#after_validate, #allow_blank, #allow_nil, #attribute, #if, #klass
Instance Method Summary collapse
Methods inherited from ValidationBase
Constructor Details
This class inherits a constructor from Validatable::ValidationBase
Instance Attribute Details
#only_integer ⇒ Object
Returns the value of attribute only_integer.
3 4 5 |
# File 'lib/validatable/validations/validates_numericality_of.rb', line 3 def only_integer @only_integer end |
Instance Method Details
#message(instance) ⇒ Object
15 16 17 |
# File 'lib/validatable/validations/validates_numericality_of.rb', line 15 def (instance) super || "must be a number" end |
#valid?(instance) ⇒ Boolean
5 6 7 8 9 10 11 12 13 |
# File 'lib/validatable/validations/validates_numericality_of.rb', line 5 def valid?(instance) value = value_for(instance) return true if allow_nil && value.nil? return true if allow_blank && (!value or (value.respond_to?(:empty?) and value.empty?)) value = value.to_s regex = self.only_integer ? /\A[+-]?\d+\Z/ : /^\d*\.{0,1}\d+$/ not (value =~ regex).nil? end |