public
Fork of sam/dm-core
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/somebee/dm-core.git
Search Repo:
Merge git://github.com/sam/dm-core into association_finders
somebee (author)
Thu May 15 03:17:43 -0700 2008
commit  9c9faa5cbf722af4e8336edb604311ce0e5cbc3c
tree    64e3154114fa1de51d13ee910ba6f89be0cb3fec
parent  5d0ce7c23948c1ecc71fe3d2639c744b8310fc96 parent  df686b12cbc192c21d28205f5a7ebeba15c2345f
...
68
69
70
71
 
 
72
73
74
75
76
77
 
78
79
80
...
82
83
84
85
86
87
88
 
 
 
89
90
91
...
111
112
113
114
115
116
117
 
 
 
118
119
120
...
135
136
137
138
139
140
 
 
 
 
 
 
 
 
 
141
142
143
144
145
146
...
200
201
202
203
204
205
 
 
206
207
 
208
209
210
211
212
213
214
 
 
215
216
 
217
218
219
...
68
69
70
 
71
72
73
74
75
76
77
 
78
79
80
81
...
83
84
85
 
 
 
 
86
87
88
89
90
91
...
111
112
113
 
 
 
 
114
115
116
117
118
119
...
134
135
136
 
 
 
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
...
205
206
207
 
 
 
208
209
210
 
211
212
213
214
215
 
 
 
216
217
218
 
219
220
221
222
0
@@ -68,13 +68,14 @@
0
     end
0
 
0
     def define_instance_or_class_method(new_meth_name, block, scope)
0
- if scope == :class
0
+ case scope
0
+ when :class
0
         class << self
0
           self
0
         end.instance_eval do
0
           define_method new_meth_name, block
0
         end
0
- elsif scope == :instance
0
+ when :instance
0
         define_method new_meth_name, block
0
       else
0
         raise ArgumentError.new("You need to pass :class or :instance as scope")
0
@@ -82,10 +83,9 @@
0
     end
0
 
0
     def hooks_with_scope(scope)
0
- if scope == :class
0
- class_method_hooks
0
- elsif scope == :instance
0
- hooks
0
+ case scope
0
+ when :class then class_method_hooks
0
+ when :instance then hooks
0
       else
0
         raise ArgumentError.new("You need to pass :class or :instance as scope")
0
       end
0
@@ -111,10 +111,9 @@
0
     end
0
 
0
     def method_with_scope(name, scope)
0
- if scope == :class
0
- method(name)
0
- elsif scope == :instance
0
- instance_method(name)
0
+ case scope
0
+ when :class then method(name)
0
+ when :instance then instance_method(name)
0
       else
0
         raise ArgumentError.new("You need to pass :class or :instance as scope")
0
       end
0
@@ -135,9 +134,15 @@
0
 
0
       <<-EOD
0
         def #{prefix}#{name}(#{args})
0
- #{inline_hooks(name, scope, types.first, args)}
0
- retval = #{inline_call(name, scope, args)}
0
- #{inline_hooks(name, scope, types.last, args)}
0
+ retval = nil
0
+ catch(:halt) do
0
+ #{inline_hooks(name, scope, types.first, args)}
0
+ retval = #{inline_call(name, scope, args)}
0
+ end
0
+
0
+ catch(:halt) do
0
+ #{inline_hooks(name, scope, types.last, args)}
0
+ end
0
           retval
0
         end
0
       EOD
0
0
0
0
@@ -200,20 +205,18 @@
0
     end
0
 
0
     def hooks
0
- return @hooks if @hooks
0
- if self.superclass.respond_to?(:hooks)
0
- @hooks = self.superclass.hooks
0
+ @hooks ||= if self.superclass.respond_to?(:hooks)
0
+ self.superclass.hooks
0
       else
0
- @hooks = Hash.new { |h, k| h[k] = {} }
0
+ Hash.new { |h, k| h[k] = {} }
0
       end
0
     end
0
 
0
     def class_method_hooks
0
- return @class_method_hooks if @class_method_hooks
0
- if self.superclass.respond_to?(:class_method_hooks)
0
- @class_method_hooks = self.superclass.class_method_hooks
0
+ @class_method_hooks ||= if self.superclass.respond_to?(:class_method_hooks)
0
+ self.superclass.class_method_hooks
0
       else
0
- @class_method_hooks = Hash.new { |h, k| h[k] = {} }
0
+ Hash.new { |h, k| h[k] = {} }
0
       end
0
     end
0
 
...
69
70
71
 
 
 
72
73
74
...
69
70
71
72
73
74
75
76
77
0
@@ -69,6 +69,9 @@
0
             identity_map_set(resource)
0
             resource.instance_variable_set(:@new_record, false)
0
             resource.dirty_attributes.clear
0
+ properties_with_indexes = Hash[*resource.class.properties.zip((0...resource.class.properties.length).to_a).flatten]
0
+ resource.collection = DataMapper::Collection.new(self, resource.class, properties_with_indexes)
0
+ resource.collection << resource
0
             success = true
0
           end
0
         else
...
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
27
28
29
...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
0
@@ -24,6 +24,18 @@
0
       orange.reload!
0
       orange.color.should == 'orange'
0
     end
0
+
0
+ it "should be able to reload new objects" do
0
+ repository(:sqlite3) do
0
+ orange = Orange.new
0
+ orange.name = 'Tom'
0
+ orange.save
0
+
0
+ lambda do
0
+ orange.reload!
0
+ end.should_not raise_error
0
+ end
0
+ end
0
 
0
     describe "anonymity" do
0
 
...
471
472
473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
0
@@ -471,5 +471,89 @@
0
       end
0
     end.should raise_error(ArgumentError)
0
   end
0
+
0
+
0
+ describe 'aborting' do
0
+ class CaptHook
0
+ include DataMapper::Resource
0
+ property :id, Integer, :key => true
0
+ property :eaten, Boolean
0
+
0
+ @@ruler_of_all_neverland = false
0
+ @@clocks_bashed = 0
0
+
0
+ def self.ruler_of_all_neverland?
0
+ @@ruler_of_all_neverland
0
+ end
0
+
0
+ def self.conquer_neverland
0
+ @@ruler_of_all_neverland = true
0
+ end
0
+
0
+ def self.bash_clock
0
+ @@clocks_bashed += 1
0
+ end
0
+
0
+ def self.clocks_bashed
0
+ @@clocks_bashed
0
+ end
0
+
0
+ def self.walk_the_plank!
0
+ true
0
+ end
0
+
0
+ def get_eaten_by_croc
0
+ self.eaten = true
0
+ end
0
+
0
+ def throw_halt
0
+ throw :halt
0
+ end
0
+ end
0
+
0
+
0
+ it "should catch :halt from a before instance hook and abort the advised method" do
0
+ CaptHook.before :get_eaten_by_croc, :throw_halt
0
+ capt_hook = CaptHook.new
0
+ lambda {
0
+ capt_hook.get_eaten_by_croc
0
+ capt_hook.should_not be_eaten
0
+ }.should_not throw_symbol(:halt)
0
+ end
0
+
0
+ it "should catch :halt from an after instance hook and cease the advice" do
0
+ CaptHook.after :get_eaten_by_croc, :throw_halt
0
+ capt_hook = CaptHook.new
0
+ lambda {
0
+ capt_hook.get_eaten_by_croc
0
+ capt_hook.should be_eaten
0
+ }.should_not throw_symbol(:halt)
0
+ end
0
+
0
+ it "should catch :halt from a before class method hook and abort advised method" do
0
+ CaptHook.before_class_method :conquer_neverland, :throw_halt
0
+ lambda {
0
+ CaptHook.conquer_neverland
0
+ CaptHook.should_not be_ruler_of_all_neverland
0
+ }.should_not throw_symbol(:halt)
0
+
0
+ end
0
+
0
+ it "should catch :halt from an after class method hook and abort the rest of the advice" do
0
+ CaptHook.after_class_method :bash_clock, :throw_halt
0
+ lambda {
0
+ CaptHook.bash_clock
0
+ CaptHook.clocks_bashed.should == 1
0
+ }.should_not throw_symbol(:halt)
0
+
0
+ end
0
+
0
+ after do
0
+ # Thus perished James Hook
0
+ CaptHook.walk_the_plank!
0
+ end
0
+ end
0
+
0
+
0
 end

Comments

    No one has commented yet.