public
Rubygem
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Updated numeric validator to raise if precision and scale are invalid
Dan Kubb (author)
Tue Jun 24 08:55:58 -0700 2008
commit  47b8a82b187f95dfb4702ebd0b99903fcf2daf44
tree    ea62155c04ac75f2762f0fc35017e816d7a5d72c
parent  e602a514b011d7d510950414b45ce5b9309c4012
...
27
28
29
 
30
31
32
33
 
 
 
34
 
 
35
36
 
37
38
39
...
27
28
29
30
31
 
 
 
32
33
34
35
36
37
38
 
39
40
41
42
0
@@ -27,13 +27,16 @@ module DataMapper
0
           return true if value =~ /\A[+-]?\d+\z/
0
           error_message ||= '%s must be an integer'.t(Extlib::Inflection.humanize(@field_name))
0
         else
0
+          # FIXME: if precision and scale are not specified, can we assume that it is an integer?
0
           if precision && scale
0
-            if precision == scale
0
-              return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
0
-            elsif scale == 0
0
+            if precision > scale && scale > 0
0
+              return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
0
+            elsif precision > scale && scale == 0
0
               return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
0
+            elsif precision == scale
0
+              return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
0
             else
0
-              return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
0
+              raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})"
0
             end
0
           else
0
             return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/

Comments