Class: Validatable::ValidatesNumericalityOf

Inherits:
ValidationBase show all
Defined in:
lib/validatable/validations/validates_numericality_of.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from ValidationBase

#attribute

Instance Method Summary collapse

Methods inherited from ValidationBase

after_validate, after_validations, all_after_validations, all_defaults, default, defaults, #i18n_prefix, #initialize, option, #raise_error_if_key_is_dup, required_option, #run_after_validate, #should_validate?, #validate_this_time?

Methods included from Requireable

included, #requires

Methods included from Understandable

included, #must_understand

Constructor Details

This class inherits a constructor from Validatable::ValidationBase

Instance Method Details

#i18nObject



19
20
21
# File 'lib/validatable/validations/validates_numericality_of.rb', line 19

def i18n
  super || "#{i18n_prefix}.not_a_number"
end

#message(instance) ⇒ Object



15
16
17
# File 'lib/validatable/validations/validates_numericality_of.rb', line 15

def message(instance)
  super || "must be a number"
end

#valid?(instance) ⇒ Boolean

Returns:

  • (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.blank?
  
  value = value.to_s
  regex = self.only_integer ? /\A[+-]?\d+\Z/ : /^\d*\.{0,1}\d+$/
  not (value =~ regex).nil?
end