mongoid_versioned_atomic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6859e384eb204360d022be2f91b8057abbe852eb
4
- data.tar.gz: 84f61d00dd36b314e501ecaba475943becea95cc
3
+ metadata.gz: d83eb64a3ce02734d1d23bb0a68774ac009088a1
4
+ data.tar.gz: 12d3ddf6e8b9c379191b86d7505c354aca79e0fc
5
5
  SHA512:
6
- metadata.gz: 8f17e52372a7ee52beac5f27daed48cdbeca51e3199642788f540e0089fc3894bd7ac42677d9a566605c2e4b08a6b59d7552cc186c207948d7b672f239ea8bc8
7
- data.tar.gz: 5adb80e64c304229f049b4b20fec32b66e6c935f49757d8c0546d5f4b89cfebac4f5e9bb12c2e6ee46bb5017de0a681df8e13f53ea94220b8fb90e8a5ce38ed2
6
+ metadata.gz: f2e65e4bc80c43b1b3683a9390cbc52a30f9f60fa793c875c63ee2b425da4f0d33cc670619b8f9fc37b17fa99d48ad0434bbb46e68aba73e55931c79bd7fa079
7
+ data.tar.gz: 7b8dbd095df5136ccb0ee7ab7abae9905fd961edd2ffe35ae2957a321e7c5f3668d01bc5b62533c448c0ce524a5d874b90ad55f261ae712e4e64ffcbb0093ffd
@@ -1,4 +1,8 @@
1
1
  module MongoidVersionedAtomic
2
+
3
+ class DbUnchanged < StandardError
4
+ end
5
+
2
6
  module VAtomic
3
7
 
4
8
  extend ActiveSupport::Concern
@@ -10,6 +14,10 @@ module MongoidVersionedAtomic
10
14
  attr_accessor :modified_count
11
15
  attr_accessor :upserted_id
12
16
  #before_save :filter_fields
17
+ after_create :check_upserted_id
18
+ after_update :check_modified_count
19
+
20
+
13
21
  end
14
22
 
15
23
  def self.included(base)
@@ -156,6 +164,15 @@ module MongoidVersionedAtomic
156
164
 
157
165
  end
158
166
 
167
+ ## after create callback , ensures that callback chain is halted if nothing was created.
168
+ def check_upserted_id
169
+ raise DbUnchanged if !self.upserted_id
170
+ end
171
+
172
+ ## after update callback ensures that callback chain is halted if nothing was modified.
173
+ def check_modified_count
174
+ raise DbUnchanged if !self.modified_count == 1
175
+ end
159
176
 
160
177
  ## @param query[Hash] : optional query hash.
161
178
  ## @param log[Boolean] : defaults to false, set true if you want to print out the final command sent to mongo.
@@ -198,44 +215,42 @@ module MongoidVersionedAtomic
198
215
 
199
216
  expected_version = 1
200
217
 
201
- prepare_insert(options) do
202
-
203
- as_document.keys.each do |k|
204
- if (k != "version" && k != "op_success")
205
- update["$setOnInsert"][k] = self.send(k.to_sym)
206
- end
207
- end
218
+ begin
219
+ prepare_insert(options) do
220
+
221
+ as_document.keys.each do |k|
222
+ if (k != "version" && k != "op_success")
223
+ update["$setOnInsert"][k] = self.send(k.to_sym)
224
+ end
225
+ end
208
226
 
209
- update["$setOnInsert"]["version"] = 1
227
+ update["$setOnInsert"]["version"] = 1
210
228
 
211
- options,update = self.class.before_persist(options,update,true)
229
+ options,update = self.class.before_persist(options,update,true)
212
230
 
213
- self.class.log_opts(query,update,options,"create",log)
214
-
215
- write_result = collection.update_one(query,update,options)
231
+ self.class.log_opts(query,update,options,"create",log)
232
+
233
+ write_result = collection.update_one(query,update,options)
216
234
 
217
235
 
218
-
219
- self.matched_count = write_result.matched_count
220
- self.modified_count = write_result.modified_count
221
- self.upserted_id = write_result.upserted_id
222
- ##as long as it matched a document, or it inserted a document
223
- if write_result.matched_count > 0 || write_result.upserted_id
224
- self.send("op_success=",true)
225
- self.version = 1
226
- else
227
- self.send("op_success",false)
228
- end
229
- #if !write_result.upserted_id.nil?
230
236
 
231
- # self.send("op_success=",true)
232
- # self.version = 1
233
- #else
234
- # self.send("op_success=",false)
235
- #end
237
+ self.matched_count = write_result.matched_count
238
+ self.modified_count = write_result.modified_count
239
+ self.upserted_id = write_result.upserted_id
240
+ ##as long as it matched a document, or it inserted a document
241
+ if write_result.matched_count > 0 || write_result.upserted_id
242
+ self.send("op_success=",true)
243
+ self.version = 1
244
+ else
245
+ self.send("op_success",false)
246
+ end
236
247
 
237
248
 
238
- end
249
+
250
+ end
251
+ rescue DbUnchanged => error
252
+ puts "caught db unchanged error, so callbacks will be halted.-------------------------------------------------------------"
253
+ end
239
254
 
240
255
  end
241
256
 
@@ -297,7 +312,7 @@ module MongoidVersionedAtomic
297
312
  expected_version = curr_doc["version"] + 1
298
313
 
299
314
  ##what happens is that we send the update["$set"][k] to whatever was stored in the dirty_fields.
300
-
315
+ begin
301
316
  prepare_update(options) do
302
317
 
303
318
  dirty_fields.keys.each do |k|
@@ -325,6 +340,9 @@ module MongoidVersionedAtomic
325
340
  end
326
341
 
327
342
  end
343
+ rescue DbUnchanged => error
344
+ #puts "Rescued db unchanged error, so remaining after update callbacks will be halted."
345
+ end
328
346
 
329
347
  end
330
348
 
@@ -1,3 +1,3 @@
1
1
  module MongoidVersionedAtomic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -37,7 +37,8 @@ class User
37
37
  end
38
38
 
39
39
  def do_after_create
40
- self.after_create_field = 1
40
+
41
+ self.after_create_field = 1
41
42
  end
42
43
 
43
44
  def do_before_update