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
Added specs for numeric validator

* Fixed bug when precision == scale
* Fix for ticket #300
Dan Kubb (author)
Sun May 25 17:39:48 -0700 2008
commit  0765e3cd805059b3d8f01b480d42918538635fa7
tree    34214d1c19071901415fe11ee8235552f3ca066a
parent  0ff796fa8fe64e1125c74bfd1be405f338fdab9d
...
84
85
86
87
88
 
 
89
90
91
...
84
85
86
 
 
87
88
89
90
91
0
@@ -84,8 +84,8 @@ module DataMapper
0
           opts[:integer_only] = true
0
           validates_is_number property.name, opts
0
         elsif BigDecimal == property.type || Float == property.type
0
-          opts[:precision] = property.precision if property.precision > 0
0
-          opts[:scale]     = property.scale     if property.scale != 10
0
+          opts[:precision] = property.precision
0
+          opts[:scale]     = property.scale
0
           validates_is_number property.name, opts
0
         end
0
       end
...
28
29
30
31
 
 
 
 
 
 
 
32
33
34
...
28
29
30
 
31
32
33
34
35
36
37
38
39
40
0
@@ -28,7 +28,13 @@ module DataMapper
0
           error_message ||= '%s must be an integer'.t(DataMapper::Inflection.humanize(@field_name))
0
         else
0
           if scale && precision
0
-            return true if value =~ /\A(?:\d{1,#{scale - precision}}|\d{0,#{scale - precision}}\.\d{1,#{precision}})\z/
0
+            if scale == precision
0
+              return true if value =~ /\A(?:0\.\d{1,#{precision}})\z/
0
+            elsif precision == 0
0
+              return true if value =~ /\A(?:\d{1,#{scale}}(?:\.0)?)\z/
0
+            else
0
+              return true if value =~ /\A(?:\d{1,#{scale - precision}}|\d{0,#{scale - precision}}\.\d{1,#{precision}})\z/
0
+            end
0
           else
0
             return true if value =~ /\A(?:\d+|\d*\.\d+)\z/
0
           end
...
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
...
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
0
@@ -53,29 +53,116 @@ describe DataMapper::Validate::NumericValidator do
0
     h.should be_valid
0
   end
0
 
0
-  it "should validate with autovalidate" do
0
-
0
-    class RobotFish
0
-      include DataMapper::Resource
0
-      property :id,     Integer, :serial => true
0
-      property :scales, Integer
0
-      property :average_weight, Float
0
+  describe 'auto validation' do
0
+    before :all do
0
+      class Fish
0
+        include DataMapper::Resource
0
+        property :id,     Integer, :serial => true
0
+        property :scales, Integer
0
+      end
0
     end
0
 
0
-    class PondFish
0
-      include DataMapper::Resource
0
-      property :id,     Integer, :serial => true
0
-      property :scales, Integer
0
-      property :average_weight, Float, :scale => 10, :precision => 0, :auto_validation => false
0
-      validates_is_number :average_weight
0
-    end
0
+    describe 'Float' do
0
+      describe 'with default scale and precision' do
0
+        before :all do
0
+          class RobotFish < Fish
0
+            property :average_weight, Float
0
+          end
0
+        end
0
 
0
-    fish1 = PondFish.new
0
-    fish2 = RobotFish.new
0
-    fish1.scales = fish2.scales = 1
0
-    fish1.average_weight = fish2.average_weight = 20.22
0
-    fish1.valid?.should == true
0
-    fish2.valid?.should == true
0
-  end
0
+        before do
0
+          @robot_fish = RobotFish.new
0
+        end
0
+
0
+        it 'should allow up to 10 digits before the decimal' do
0
+          @robot_fish.average_weight = 0
0
+          @robot_fish.should be_valid
0
+
0
+          @robot_fish.average_weight = 9_999_999_999
0
+          @robot_fish.should be_valid
0
+
0
+          @robot_fish.average_weight = 10_000_000_000
0
+          @robot_fish.should_not be_valid
0
+        end
0
+
0
+        it 'should allow 0 digits of precision after the decimal' do
0
+          @robot_fish.average_weight = 0
0
+          @robot_fish.should be_valid
0
+        end
0
+
0
+        it 'should allow 1 digit of precision after the decimal if it is a zero' do
0
+          @robot_fish.average_weight = 0.0
0
+          @robot_fish.should be_valid
0
+
0
+          @robot_fish.average_weight = 9_999_999_999.0
0
+          @robot_fish.should be_valid
0
+
0
+          @robot_fish.average_weight = 0.1
0
+          @robot_fish.should_not be_valid
0
+        end
0
+      end
0
+
0
+      describe 'with a scale of 4 and a precision of 2' do
0
+        before :all do
0
+          class GoldFish < Fish
0
+            property :average_weight, Float, :scale => 4, :precision => 2
0
+          end
0
+        end
0
+
0
+        before do
0
+          @gold_fish = GoldFish.new
0
+        end
0
+
0
+        it 'should allow up to 2 digits before the decimal' do
0
+          @gold_fish.average_weight = 0
0
+          @gold_fish.should be_valid
0
 
0
+          @gold_fish.average_weight = 99
0
+          @gold_fish.should be_valid
0
+
0
+          @gold_fish.average_weight = 100
0
+          @gold_fish.should_not be_valid
0
+        end
0
+
0
+        it 'should allow 2 digits of precision after the decimal' do
0
+          @gold_fish.average_weight = 99.99
0
+          @gold_fish.should be_valid
0
+
0
+          @gold_fish.average_weight = 99.999
0
+          @gold_fish.should_not be_valid
0
+        end
0
+      end
0
+
0
+      describe 'with a scale of 2 and a precision of 2' do
0
+        before :all do
0
+          class SilverFish < Fish
0
+            property :average_weight, Float, :scale => 2, :precision => 2
0
+          end
0
+        end
0
+
0
+        before do
0
+          @silver_fish = SilverFish.new
0
+        end
0
+
0
+        it 'should allow a 0 before the decimal' do
0
+          @silver_fish.average_weight = 0
0
+          @silver_fish.should be_valid
0
+
0
+          @silver_fish.average_weight = 0.1
0
+          @silver_fish.should be_valid
0
+
0
+          @silver_fish.average_weight = 1
0
+          @silver_fish.should_not be_valid
0
+        end
0
+
0
+        it 'should allow 2 digits of precision after the decimal' do
0
+          @silver_fish.average_weight = 0.99
0
+          @silver_fish.should be_valid
0
+
0
+          @silver_fish.average_weight = 0.999
0
+          @silver_fish.should_not be_valid
0
+        end
0
+      end
0
+    end
0
+  end
0
 end

Comments