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
accounting for further defaults
Adam French (author)
Wed May 07 10:29:49 -0700 2008
commit  e3d72a8a9f1dd7fc68500c539e362444d6b118c6
tree    9c61974e6af30deed99d2ec9bb89f4ca411b2b0b
parent  a55dc09669177299de9d8c6f9d4a67bc8e9db980
...
9
10
11
12
 
13
14
15
...
9
10
11
 
12
13
14
15
0
@@ -9,7 +9,7 @@ module DataMapper
0
       end
0
       
0
       def call(target)
0
-        field_value = target.instance_variable_get("@#{@field_name}").blank?
0
+        field_value = target.attribute_get(field_name).blank?
0
         return true if field_value
0
         
0
         error_message = @options[:message] || "%s must be absent".t(DataMapper::Inflection.humanize(@field_name))
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@ module DataMapper
0
       end
0
       
0
       def call(target)      
0
-        value =  target.instance_variable_get("@#{@field_name}").to_s
0
+        value =  target.attribute_get(field_name).to_s
0
         regex = @options[:integer_only] ? /\A[+-]?\d+\Z/ : /^\d*\.{0,1}\d+$/
0
         return true if not (value =~ regex).nil?
0
         
...
412
413
414
 
 
 
 
 
 
 
 
415
416
417
...
431
432
433
 
 
 
 
434
435
436
...
444
445
446
 
 
 
 
 
 
 
447
448
449
...
454
455
456
 
 
 
 
 
457
458
459
...
556
557
558
559
 
560
561
562
...
567
568
569
570
 
571
572
573
...
740
741
742
 
 
 
 
 
 
 
743
744
745
...
812
813
814
 
 
 
 
 
815
816
817
...
890
891
892
 
 
 
 
 
 
 
 
893
894
895
...
923
924
925
 
 
 
 
 
926
927
928
...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
...
439
440
441
442
443
444
445
446
447
448
...
456
457
458
459
460
461
462
463
464
465
466
467
468
...
473
474
475
476
477
478
479
480
481
482
483
...
580
581
582
 
583
584
585
586
...
591
592
593
 
594
595
596
597
...
764
765
766
767
768
769
770
771
772
773
774
775
776
...
843
844
845
846
847
848
849
850
851
852
853
...
926
927
928
929
930
931
932
933
934
935
936
937
938
939
...
967
968
969
970
971
972
973
974
975
976
977
0
@@ -412,6 +412,14 @@ begin
0
         validates_presence_of :name, :when => :property_test    
0
         validates_presence_of :landscaper, :when => :association_test    
0
       end
0
+      
0
+      class Fertilizer
0
+        include DataMapper::Resource
0
+        include DataMapper::Validate
0
+        property :id, Fixnum, :serial => true
0
+        property :brand, String, :auto_validation => false, :default => 'Scotts'
0
+        validates_presence_of :brand, :when => :property_test
0
+      end
0
     end
0
 
0
     it "should validate the presence of a property value on an instance of a resource" do
0
@@ -431,6 +439,10 @@ begin
0
     #  #puts "Gardens landscaper is #{garden.landscaper.child_key}"
0
     #end
0
     
0
+    it "should pass when a default is available" do
0
+      fert = Fertilizer.new
0
+      fert.should be_valid_for_property_test
0
+    end
0
   end
0
 
0
 
0
@@ -444,6 +456,13 @@ begin
0
               
0
         validates_absence_of :salesman, :when => :sold    
0
       end
0
+      
0
+      class Pirogue
0
+        include DataMapper::Resource
0
+        include DataMapper::Validate
0
+        property :salesman, String, :default => 'Layfayette'
0
+        validates_absence_of :salesman, :when => :sold
0
+      end
0
     end
0
 
0
     it "should validate the absense of a value on an instance of a resource" do
0
@@ -454,6 +473,11 @@ begin
0
       kayak.valid_for_sold?.should_not == true    
0
     end
0
     
0
+    it "should validate the absense of a value and ensure defaults" do
0
+      pirogue = Pirogue.new
0
+      pirogue.should_not be_valid_for_sold
0
+    end
0
+    
0
   end
0
 
0
   #-------------------------------------------------------------------------------
0
@@ -556,7 +580,7 @@ begin
0
       end
0
     end
0
     
0
-    it "should validate the confrimation of a value on an instance of a resource" do
0
+    it "should validate the confirmation of a value on an instance of a resource" do
0
       canoe = Canoe.new
0
       canoe.name = 'White Water'
0
       canoe.name_confirmation = 'Not confirmed'
0
@@ -567,7 +591,7 @@ begin
0
       canoe.valid?.should == true
0
     end
0
     
0
-    it "should default the name of the confirimation field to <field>_confirmation if one is not specified" do
0
+    it "should default the name of the confirmation field to <field>_confirmation if one is not specified" do
0
       canoe = Canoe.new
0
       canoe.name = 'White Water'
0
       canoe.name_confirmation = 'White Water'
0
@@ -740,6 +764,13 @@ end
0
         include DataMapper::Validate     
0
         property :name, String, :auto_validation => false   
0
       end
0
+      
0
+      class BoatDock
0
+        include DataMapper::Resource
0
+        include DataMapper::Validate
0
+        property :name, String, :auto_validation => false, :default => "I'm a long string"
0
+        validates_length_of :name, :min => 3
0
+      end
0
     end
0
 
0
     it 'should be able to set a minimum length of a string field' do
0
@@ -812,6 +843,11 @@ end
0
       launch.name = 'Ride'
0
       launch.valid?.should == true      
0
     end  
0
+    
0
+    it "should pass if a default fufills the requirements" do
0
+      doc = BoatDock.new
0
+      doc.should be_valid
0
+    end
0
   end
0
 
0
   #-------------------------------------------------------------------------------
0
@@ -890,6 +926,14 @@ end
0
         
0
         validates_numericalnes_of :amount_1, :amount_2      
0
       end
0
+      
0
+      class Hillary
0
+        include DataMapper::Resource
0
+        include DataMapper::Validate
0
+        property :amount_1, Float, :auto_validation => false, :default => 0.01
0
+        validates_numericalnes_of :amount_1
0
+        
0
+      end
0
     end
0
     
0
     it "should validate a floating point value on the instance of a resource" do
0
@@ -923,6 +967,11 @@ end
0
       
0
     end
0
     
0
+    it "should validate if a default fufills the requirements" do
0
+      h = Hillary.new
0
+      h.should be_valid
0
+    end
0
+    
0
   end  
0
 
0
 

Comments