Class: CouchRest::Validation::NumericValidator

Inherits:
GenericValidator show all
Defined in:
lib/couchrest/validation/validators/numeric_validator.rb

Overview

Author:

  • Guy van den Berg

Since:

  • 0.9

Instance Attribute Summary

Attributes inherited from GenericValidator

#field_name, #if_clause, #unless_clause

Instance Method Summary collapse

Methods inherited from GenericValidator

#==, #add_error, #execute?

Constructor Details

#initialize(field_name, options = {}) ⇒ NumericValidator

Returns a new instance of NumericValidator.

Since:

  • 0.9



33
34
35
36
37
# File 'lib/couchrest/validation/validators/numeric_validator.rb', line 33

def initialize(field_name, options={})
  super
  @field_name, @options = field_name, options
  @options[:integer_only] = false unless @options.has_key?(:integer_only)
end

Instance Method Details

#call(target) ⇒ Object

Since:

  • 0.9



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/couchrest/validation/validators/numeric_validator.rb', line 39

def call(target)
  value = target.send(field_name)
  return true if @options[:allow_nil] && value.nil?

  value = value.kind_of?(Float) ? value.to_s('F') : value.to_s

  error_message = @options[:message]
  precision     = @options[:precision]
  scale         = @options[:scale]

  if @options[:integer_only]
    return true if value =~ /\A[+-]?\d+\z/
    error_message ||= ValidationErrors.default_error_message(:not_an_integer, field_name)
  else
    # FIXME: if precision and scale are not specified, can we assume that it is an integer?
    #        probably not, as floating point numbers don't have hard
    #        defined scale. the scale floats with the length of the
    #        integral and precision. Ie. if precision = 10 and integral
    #        portion of the number is 9834 (4 digits), the max scale will
    #        be 6 (10 - 4). But if the integral length is 1, max scale
    #        will be (10 - 1) = 9, so 1.234567890.
    if precision && scale
      #handles both Float when it has scale specified and BigDecimal
      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
    elsif precision && scale.nil?
      # for floats, if scale is not set

      #total number of digits is less or equal precision
      return true if value.gsub(/[^\d]/, '').length <= precision

      #number of digits before decimal == precision, and the number is x.0. same as scale = 0
      return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
    else
      return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/
    end
    error_message ||= ValidationErrors.default_error_message(:not_a_number, field_name)
  end

  add_error(target, error_message, field_name)

  # TODO: check the gt, gte, lt, lte, and eq options

  return false
end