mongodb 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mongo/object/object.rb +45 -30
- data/spec/object/callbacks_spec.rb +4 -4
- data/spec/object/spec_helper.rb +7 -2
- metadata +1 -1
data/lib/mongo/object/object.rb
CHANGED
@@ -4,13 +4,12 @@ module Mongo::Object
|
|
4
4
|
def valid? opts = {}
|
5
5
|
opts = ::Mongo::Object.parse_options opts
|
6
6
|
begin
|
7
|
-
return false
|
8
|
-
|
9
|
-
return false if opts[:callbacks] and !::Mongo::Object.run_callbacks(self, :before, :validate)
|
7
|
+
return false if opts[:callbacks] and !::Mongo::Object.run_before_callbacks(self, :validate)
|
10
8
|
|
11
9
|
child_opts = opts.merge internal: true
|
12
|
-
|
13
|
-
|
10
|
+
children_valid = child_objects.all?{|group| group.all?{|obj| obj.valid?(child_opts)}}
|
11
|
+
if children_valid and ::Mongo::Object.run_validations(self) and errors.empty?
|
12
|
+
::Mongo::Object.run_after_callbacks(self, :validate) if opts[:callbacks]
|
14
13
|
true
|
15
14
|
else
|
16
15
|
false
|
@@ -136,12 +135,30 @@ module Mongo::Object
|
|
136
135
|
def build doc
|
137
136
|
return unless doc
|
138
137
|
obj = _build doc
|
139
|
-
obj.send :
|
138
|
+
obj.send :update_original_children! if obj.is_a? ::Mongo::Object
|
140
139
|
obj
|
141
140
|
end
|
142
141
|
|
143
|
-
def
|
144
|
-
obj.respond_to?(:
|
142
|
+
def run_before_callbacks obj, method
|
143
|
+
if obj.respond_to?(:run_before_callbacks)
|
144
|
+
obj.run_before_callbacks(:save, method: :save) if method == :update or method == :create
|
145
|
+
obj.run_before_callbacks(method, method: method)
|
146
|
+
else
|
147
|
+
true
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def run_after_callbacks obj, method
|
152
|
+
if obj.respond_to?(:run_before_callbacks)
|
153
|
+
obj.run_after_callbacks(method, method: method)
|
154
|
+
obj.run_after_callbacks(:save, method: :save) if method == :update or method == :create
|
155
|
+
else
|
156
|
+
true
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def run_validations obj
|
161
|
+
obj.respond_to?(:run_validations) ? obj.run_validations : true
|
145
162
|
end
|
146
163
|
|
147
164
|
protected
|
@@ -183,17 +200,15 @@ module Mongo::Object
|
|
183
200
|
end
|
184
201
|
|
185
202
|
protected
|
186
|
-
attr_writer :
|
187
|
-
def _original_children
|
188
|
-
@_original_children ||= []
|
189
|
-
end
|
203
|
+
attr_writer :original_children
|
204
|
+
def original_children; @_original_children ||= [] end
|
190
205
|
|
191
|
-
def
|
206
|
+
def update_original_children!
|
192
207
|
return unless ::Mongo.defaults[:callbacks]
|
193
208
|
|
194
|
-
|
209
|
+
original_children.clear
|
195
210
|
::Mongo::Object.each_object self, false do |obj|
|
196
|
-
|
211
|
+
original_children << obj
|
197
212
|
end
|
198
213
|
end
|
199
214
|
|
@@ -210,34 +225,34 @@ module Mongo::Object
|
|
210
225
|
unless @_child_objects
|
211
226
|
created_children, updated_children, destroyed_children = [], [], []
|
212
227
|
|
213
|
-
original_children_ids = Set.new;
|
228
|
+
original_children_ids = Set.new; original_children.each{|obj| original_children_ids << obj.object_id}
|
214
229
|
::Mongo::Object.each_object self, false do |obj|
|
215
230
|
(original_children_ids.include?(obj.object_id) ? updated_children : created_children) << obj
|
216
231
|
end
|
217
232
|
|
218
233
|
children_ids = Set.new; ::Mongo::Object.each_object(self, false){|obj| children_ids << obj.object_id}
|
219
|
-
destroyed_children =
|
234
|
+
destroyed_children = original_children.select{|obj| !children_ids.include?(obj.object_id)}
|
220
235
|
|
221
236
|
@_child_objects = [created_children, updated_children, destroyed_children]
|
222
237
|
end
|
223
238
|
@_child_objects
|
224
239
|
end
|
225
240
|
|
226
|
-
def with_object_callbacks
|
241
|
+
def with_object_callbacks method, opts, &block
|
227
242
|
opts = ::Mongo::Object.parse_options opts
|
228
243
|
|
229
244
|
# validation
|
230
245
|
return false if opts[:validate] and !valid?(opts.merge(internal: true))
|
231
246
|
|
232
247
|
# before callbacks
|
233
|
-
return false if opts[:callbacks] and !run_all_callbacks(:before,
|
248
|
+
return false if opts[:callbacks] and !run_all_callbacks(:before, method)
|
234
249
|
|
235
250
|
# saving
|
236
251
|
block.call ::Mongo::Object.to_mongo_options(opts)
|
237
|
-
|
252
|
+
update_original_children!
|
238
253
|
|
239
254
|
# after callbacks
|
240
|
-
run_all_callbacks :after,
|
255
|
+
run_all_callbacks :after, method if opts[:callbacks]
|
241
256
|
|
242
257
|
true
|
243
258
|
ensure
|
@@ -245,36 +260,36 @@ module Mongo::Object
|
|
245
260
|
end
|
246
261
|
|
247
262
|
# TODO1 move to static method
|
248
|
-
def run_all_callbacks type,
|
263
|
+
def run_all_callbacks type, method
|
249
264
|
result = if type == :before
|
250
|
-
::Mongo::Object.
|
265
|
+
::Mongo::Object.run_before_callbacks self, method
|
251
266
|
else
|
252
267
|
true
|
253
268
|
end
|
254
269
|
|
255
|
-
result &= if
|
270
|
+
result &= if method == :create
|
256
271
|
child_objects.all? do |group|
|
257
272
|
group.all? do |obj|
|
258
|
-
obj.run_all_callbacks type,
|
273
|
+
obj.run_all_callbacks type, method
|
259
274
|
end
|
260
275
|
end
|
261
|
-
elsif
|
276
|
+
elsif method == :update
|
262
277
|
created_children, updated_children, destroyed_children = child_objects
|
263
278
|
created_children.all?{|obj| obj.run_all_callbacks type, :create} and
|
264
279
|
updated_children.all?{|obj| obj.run_all_callbacks type, :update} and
|
265
280
|
destroyed_children.all?{|obj| obj.run_all_callbacks type, :destroy}
|
266
|
-
elsif
|
281
|
+
elsif method == :destroy
|
267
282
|
child_objects.all? do |group|
|
268
283
|
group.all? do |obj|
|
269
|
-
obj.run_all_callbacks type,
|
284
|
+
obj.run_all_callbacks type, method
|
270
285
|
end
|
271
286
|
end
|
272
287
|
else
|
273
|
-
raise_error "unknown callback method (#{
|
288
|
+
raise_error "unknown callback method (#{method})!"
|
274
289
|
end
|
275
290
|
|
276
291
|
result &= if type == :after
|
277
|
-
::Mongo::Object.
|
292
|
+
::Mongo::Object.run_after_callbacks self, method
|
278
293
|
else
|
279
294
|
true
|
280
295
|
end
|
@@ -22,7 +22,7 @@ describe 'Object callbacks' do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'create' do
|
25
|
-
%w(before_validate after_validate before_create after_create).each do |name|
|
25
|
+
%w(before_validate after_validate before_save before_create after_create after_save).each do |name|
|
26
26
|
@player.should_receive(name).once.ordered.and_return(true)
|
27
27
|
@mission.should_receive(name).once.ordered.and_return(true)
|
28
28
|
end
|
@@ -33,7 +33,7 @@ describe 'Object callbacks' do
|
|
33
33
|
it 'update' do
|
34
34
|
db.players.save(@player).should be_true
|
35
35
|
|
36
|
-
%w(before_validate after_validate before_update after_update).each do |name|
|
36
|
+
%w(before_validate after_validate before_save before_update after_update after_save).each do |name|
|
37
37
|
@player.should_receive(name).once.ordered.and_return(true)
|
38
38
|
@mission.should_receive(name).once.ordered.and_return(true)
|
39
39
|
end
|
@@ -63,8 +63,8 @@ describe 'Object callbacks' do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'should be able interrupt CRUD' do
|
66
|
-
@mission.stub! :
|
67
|
-
false if
|
66
|
+
@mission.stub! :run_before_callbacks do |method_name|
|
67
|
+
false if method_name == :create
|
68
68
|
end
|
69
69
|
db.players.save(@player).should be_false
|
70
70
|
db.players.count.should == 0
|
data/spec/object/spec_helper.rb
CHANGED
@@ -7,8 +7,13 @@ require 'driver/spec_helper'
|
|
7
7
|
|
8
8
|
# To simplify callback expectations
|
9
9
|
module RSpec::CallbackHelper
|
10
|
-
def
|
11
|
-
callback_method_name = :"#{
|
10
|
+
def run_before_callbacks method_name, opts = {}
|
11
|
+
callback_method_name = :"before_#{method_name}"
|
12
|
+
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
13
|
+
end
|
14
|
+
|
15
|
+
def run_after_callbacks method_name, opts = {}
|
16
|
+
callback_method_name = :"after_#{method_name}"
|
12
17
|
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
13
18
|
end
|
14
19
|
end
|