Class: WhyValidationsSuckIn96::ValidatesNumericality

Inherits:
WhyValidationsSuckIn96::Validation show all
Includes:
WhyValidationsSuckIn96::AttributeBasedValidation, WhyValidationsSuckIn96::SkippableValidation
Defined in:
lib/whyvalidationssuckin96/macros/validates_numericality.rb

Overview

Checks to see if a given attribute is a valid numerical value. Allows a certain degree of latitude in determining what is a valid numerical value, like allowing commas and spaces in the value for example.

Examples:

Default usage

  setup_validations do
    validates_numericality_of :cost
  end

Only allow integer values

  setup_validations do
    validates_numericality_of :cost, :only_integer => true
  end

Constant Summary

DefaultOptions =
{:message => "is not a numerical value"}
StripNonNumeric =
/[^-+e\d.]/im
ValidNumericChars =
/\A[-+e\d.,\s]+\Z/im

Constants inherited from WhyValidationsSuckIn96::Validation

DefaultOptions

Instance Method Summary

Methods included from WhyValidationsSuckIn96::AttributeBasedValidation

#attribute, #attribute_value, #initialize

Methods inherited from WhyValidationsSuckIn96::Validation

#failed?, #has_run?, #initialize, #message, new_subclass, #passed?, #validates?

Constructor Details

This class inherits a constructor from WhyValidationsSuckIn96::AttributeBasedValidation

Instance Method Details

- (Object) validate



26
27
28
29
30
31
32
33
# File 'lib/whyvalidationssuckin96/macros/validates_numericality.rb', line 26

def validate
  super
  if options[:only_integer]
    validate_as_integer
  else
    validate_as_float
  end
end