public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Allow use of hooks defined in a superclass with an inherited class

* Useful in cases of Single Table Inheritance
* Fixes issue # 278

Signed-off-by: Alex Coles <alexbcoles@mac.com>
myabc (author)
Tue May 13 11:13:17 -0700 2008
commit  613a3676c8c312cf4385cfc142c0741f51deda33
tree    fc853423ec6845ac56a7d069d7a83e802da6ee6e
parent  0f57b64a8a8b0d036dd9cee602ceb448ac6a4e88
...
200
201
202
203
 
 
 
 
 
 
204
205
206
207
 
 
 
 
 
 
208
209
210
...
200
201
202
 
203
204
205
206
207
208
209
210
211
 
212
213
214
215
216
217
218
219
220
0
@@ -200,11 +200,21 @@ module DataMapper
0
     end
0
 
0
     def hooks
0
-      @hooks ||= Hash.new { |h, k| h[k] = {} }
0
+      return @hooks if @hooks
0
+      if self.superclass != Object
0
+        @hooks = self.superclass.hooks
0
+      else
0
+        @hooks = Hash.new { |h, k| h[k] = {} }
0
+      end
0
     end
0
 
0
     def class_method_hooks
0
-      @class_method_hooks ||= Hash.new { |h, k| h[k] = {} }
0
+      return @class_method_hooks if @class_method_hooks
0
+      if self.superclass != Object
0
+        @class_method_hooks = self.superclass.class_method_hooks
0
+      else
0
+        @class_method_hooks = Hash.new { |h, k| h[k] = {} }
0
+      end
0
     end
0
 
0
     def quote_method(name)
...
52
53
54
 
 
 
 
 
 
 
 
 
 
 
 
 
55
56
57
...
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
 
65
66
67
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
0
@@ -52,6 +52,19 @@ describe "DataMapper::Hook" do
0
     @class.a_class_method
0
   end
0
 
0
+  it 'should run an advice block for class methods when the class is inherited' do
0
+    @inherited_class = Class.new(@class)
0
+    
0
+    @class.before_class_method :a_class_method do
0
+      hi_dad!
0
+    end
0
+
0
+    @inherited_class.should_receive(:hi_dad!)
0
+
0
+    @inherited_class.a_class_method
0
+  end
0
+
0
+
0
   it 'should run an advice block' do
0
     @class.before :a_method do
0
       hi_mom!
0
@@ -62,6 +75,19 @@ describe "DataMapper::Hook" do
0
 
0
     inst.a_method
0
   end
0
+  
0
+  it 'should run an advice block when the class is inherited' do
0
+    @inherited_class = Class.new(@class)
0
+    
0
+    @class.before :a_method do
0
+      hi_dad!
0
+    end
0
+
0
+    inst = @inherited_class.new
0
+    inst.should_receive(:hi_dad!)
0
+
0
+    inst.a_method
0
+  end
0
 
0
   it 'should run an advice method' do
0
     @class.class_eval do

Comments