Class: DataMapper::Validate::NumericValidator
- Inherits:
-
GenericValidator
- Object
- GenericValidator
- DataMapper::Validate::NumericValidator
- Defined in:
- lib/dm-validations/numeric_validator.rb
Overview
Instance Attribute Summary
Attributes inherited from GenericValidator
Instance Method Summary collapse
- #call(target) ⇒ Object
-
#initialize(field_name, options = {}) ⇒ NumericValidator
constructor
A new instance of NumericValidator.
Methods inherited from GenericValidator
#add_error, #execute?, #field_name
Constructor Details
#initialize(field_name, options = {}) ⇒ NumericValidator
Returns a new instance of NumericValidator.
10 11 12 13 14 |
# File 'lib/dm-validations/numeric_validator.rb', line 10 def initialize(field_name, ={}) super @field_name, @options = field_name, @options[:integer_only] = false unless @options.has_key?(:integer_only) end |
Instance Method Details
#call(target) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dm-validations/numeric_validator.rb', line 16 def call(target) value = target.send(field_name) return true if @options[:allow_nil] && value.nil? value = value.kind_of?(BigDecimal) ? value.to_s('F') : value.to_s = @options[:message] precision = @options[:precision] scale = @options[:scale] if @options[:integer_only] return true if value =~ /\A[+-]?\d+\z/ ||= '%s must be an integer'.t(Extlib::Inflection.humanize(@field_name)) else # FIXME: if precision and scale are not specified, can we assume that it is an integer? if precision && scale if precision > scale && scale > 0 return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/ elsif precision > scale && scale == 0 return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/ elsif precision == scale return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/ else raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})" end else return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/ end ||= '%s must be a number'.t(Extlib::Inflection.humanize(@field_name)) end add_error(target, , @field_name) # TODO: check the gt, gte, lt, lte, and eq options return false end |