public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Added support to pass property information own to custom types in the dump/load
Ken Robertson (author)
Thu Apr 10 12:59:01 -0700 2008
commit  0e283f878c5bd0a3cb241a553ba2c967e17b3e91
tree    e564126ab9eb0e762353cc92333cb00e87a9ccfe
parent  0f38788ebd9dfa4395b021255b13ee7b55a58cb5
...
310
311
312
313
 
314
315
316
...
324
325
326
327
 
328
329
330
...
310
311
312
 
313
314
315
316
...
324
325
326
 
327
328
329
330
0
@@ -310,7 +310,7 @@ module DataMapper
0
               @loaded_set.reload!(:fields => self.class.properties(self.class.repository.name).lazy_load_context(#{name.inspect}))
0
             end
0
           end
0
-          #{custom? ? "#{@type.inspect}.load(#{@instance_variable_name})" : @instance_variable_name}
0
+          #{custom? ? "#{@type.inspect}.load(#{@instance_variable_name}, self.class.properties(self.class.repository.name)[#{name.inspect}])" : @instance_variable_name}
0
         end
0
       EOS
0
     rescue SyntaxError
0
@@ -324,7 +324,7 @@ module DataMapper
0
         def #{name}=(value)
0
           #{lock? ? "@shadow_#{name} = #{@instance_variable_name}" : ''}
0
           dirty_attributes << self.class.properties(repository.name)[#{name.inspect}]
0
-          #{@instance_variable_name} = #{custom? ? "#{@type.inspect}.dump(value)" : 'value'}
0
+          #{@instance_variable_name} = #{custom? ? "#{@type.inspect}.dump(value, self.class.properties(self.class.repository.name)[#{name.inspect}])" : 'value'}
0
         end
0
       EOS
0
     rescue SyntaxError
...
50
51
52
53
 
54
55
56
...
62
63
64
65
 
66
67
68
...
50
51
52
 
53
54
55
56
...
62
63
64
 
65
66
67
68
0
@@ -50,7 +50,7 @@ module DataMapper
0
       end
0
 
0
       value = instance_variable_get(ivar_name)
0
-      property.custom? ? property.type.load(value) : value
0
+      property.custom? ? property.type.load(value, property) : value
0
     end
0
 
0
     def []=(name, value)
0
@@ -62,7 +62,7 @@ module DataMapper
0
       end
0
 
0
       dirty_attributes << property
0
-      instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : value)
0
+      instance_variable_set(ivar_name, property.custom? ? property.type.dump(value, property) : value)
0
     end
0
 
0
     def repository
...
27
28
29
30
 
31
32
33
...
129
130
131
132
 
133
134
135
...
144
145
146
147
 
148
149
150
...
27
28
29
 
30
31
32
33
...
129
130
131
 
132
133
134
135
...
144
145
146
 
147
148
149
150
0
@@ -27,7 +27,7 @@ module DataMapper
0
   #     primitive String
0
   #     size 10
0
   #
0
-  #     def self.dump(value)
0
+  #     def self.dump(value, property)
0
   #       <work some magic>
0
   #     end
0
   #
0
@@ -129,7 +129,7 @@ module DataMapper
0
     #
0
     #
0
     # @public
0
-    def self.dump(value)
0
+    def self.dump(value, property)
0
       value
0
     end
0
 
0
@@ -144,7 +144,7 @@ module DataMapper
0
     #
0
     #
0
     # @public
0
-    def self.load(value)
0
+    def self.load(value, property)
0
       value
0
     end
0
 
...
5
6
7
8
 
9
10
11
...
13
14
15
16
 
17
18
19
...
5
6
7
 
8
9
10
11
...
13
14
15
 
16
17
18
19
0
@@ -5,7 +5,7 @@ module DataMapper
0
       size 65535
0
       lazy true
0
 
0
-      def self.load(value)
0
+      def self.load(value, property)
0
         case value
0
         when String then FasterCSV.parse(value)
0
         when Array then value
0
@@ -13,7 +13,7 @@ module DataMapper
0
         end
0
       end
0
 
0
-      def self.dump(value)
0
+      def self.dump(value, property)
0
         case value
0
         when Array then
0
           FasterCSV.generate do |csv|
...
7
8
9
10
 
11
12
13
...
17
18
19
20
 
21
22
23
...
7
8
9
 
10
11
12
13
...
17
18
19
 
20
21
22
23
0
@@ -7,7 +7,7 @@ module DataMapper
0
       size 65535
0
       lazy true
0
 
0
-      def self.load(value)
0
+      def self.load(value, property)
0
         if value.nil?
0
           nil
0
         elsif value.is_a?(String)
0
@@ -17,7 +17,7 @@ module DataMapper
0
         end
0
       end
0
 
0
-      def self.dump(value)
0
+      def self.dump(value, property)
0
         if value.nil?
0
           nil
0
         elsif value.is_a?(String) && value =~ /^---/
...
15
16
17
18
 
19
20
21
22
 
23
24
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
27
28
...
48
49
50
51
 
52
53
54
55
 
56
57
58
59
 
60
61
62
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
65
66
...
15
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
...
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
0
@@ -15,14 +15,35 @@ describe DataMapper::Type do
0
       primitive String
0
       size 10
0
 
0
-      def self.load(value)
0
+      def self.load(value, property)
0
         value.reverse
0
       end
0
 
0
-      def self.dump(value)
0
+      def self.dump(value, property)
0
         value.reverse
0
       end
0
     end
0
+    
0
+    class TestResource
0
+      include DataMapper::Resource
0
+    end
0
+    
0
+    class TestType3 < DataMapper::Type
0
+      primitive String
0
+      size 10
0
+      attr_accessor :property, :value
0
+
0
+      def self.load(value, property)
0
+        type = self.new
0
+        type.property = property
0
+        type.value    = value
0
+        type
0
+      end
0
+
0
+      def self.dump(value, property)
0
+        value.value
0
+      end
0
+    end
0
   end
0
 
0
   it "should have the same PROPERTY_OPTIONS aray as DataMapper::Property" do
0
@@ -48,19 +69,42 @@ describe DataMapper::Type do
0
   end
0
 
0
   it "should pass through the value if load wasn't overriden" do
0
-    TestType.load("test").should == "test"
0
+    TestType.load("test", nil).should == "test"
0
   end
0
 
0
   it "should pass through the value if dump wasn't overriden" do
0
-    TestType.dump("test").should == "test"
0
+    TestType.dump("test", nil).should == "test"
0
   end
0
 
0
   it "should not raise NotImplmenetedException if load was overriden" do
0
-    TestType2.dump("helo").should == "oleh"
0
+    TestType2.dump("helo", nil).should == "oleh"
0
   end
0
 
0
   it "should not raise NotImplmenetedException if dump was overriden" do
0
-    TestType2.load("oleh").should == "helo"
0
+    TestType2.load("oleh", nil).should == "helo"
0
+  end
0
+
0
+  describe "using a custom type" do
0
+    before do
0
+      @property = DataMapper::Property.new TestResource, :name, TestType3, {}
0
+    end
0
+    
0
+    it "should return a object of the same type" do
0
+      TestType3.load("helo", @property).class.should == TestType3
0
+    end
0
+    
0
+    it "should contain the property" do
0
+      TestType3.load("helo", @property).property.should == @property
0
+    end
0
+    
0
+    it "should contain the value" do
0
+      TestType3.load("helo", @property).value.should == "helo"
0
+    end
0
+    
0
+    it "should return the value" do
0
+      obj = TestType3.load("helo", @property)
0
+      TestType3.dump(obj, @property).should == "helo"
0
+    end
0
   end
0
 
0
   describe "using def Type" do

Comments