mongomatic 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -174,10 +174,19 @@ module Mongomatic
174
174
  # Insert the document into the database. Will return false if the document has
175
175
  # already been inserted or is invalid. Returns the generated BSON::ObjectId
176
176
  # for the new document. Will silently fail if MongoDB is unable to insert the
177
- # document, use insert! if you want an error raised instead. Note that this will
178
- # require an additional call to the db.
177
+ # document, use insert! or send in {:safe => true} if you want a Mongo::OperationError.
178
+ # If you want to raise the following errors also, pass in {:raise => true}
179
+ # * Raises Mongomatic::Exceptions::DocumentNotNew if document is not new
180
+ # * Raises Mongomatic::Exceptions::DocumentNotValid if there are validation errors
179
181
  def insert(opts={})
180
- return false unless new? && valid?
182
+ if opts[:raise] == true
183
+ raise Mongomatic::Exceptions::DocumentWasRemoved if removed?
184
+ raise Mongomatic::Exceptions::DocumentNotNew unless new?
185
+ raise Mongomatic::Exceptions::DocumentNotValid unless valid?
186
+ else
187
+ return false unless new? && valid?
188
+ end
189
+
181
190
  do_callback(:before_insert)
182
191
  do_callback(:before_insert_or_update)
183
192
  if ret = self.class.collection.insert(@doc,opts)
@@ -189,18 +198,30 @@ module Mongomatic
189
198
  ret
190
199
  end
191
200
 
192
- # Calls insert(...) with {:safe => true} passed in as an option. Will check MongoDB
193
- # after insert to make sure that the insert was successful, and raise a Mongo::OperationError
194
- # if there were any problems.
201
+ # Calls insert(...) with {:safe => true} passed in as an option.
202
+ # * Raises Mongo::OperationError if there was a DB error on inserting
203
+ # If you want to raise the following errors also, pass in {:raise => true}
204
+ # * Raises Mongomatic::Exceptions::DocumentNotNew if document is not new
205
+ # * Raises Mongomatic::Exceptions::DocumentNotValid if there are validation errors
195
206
  def insert!(opts={})
196
207
  insert(opts.merge(:safe => true))
197
208
  end
198
209
 
199
- # Will persist any changes you have made to the document. Will silently fail if
200
- # MongoDB is unable to update the document, use update! instead if you want an
201
- # error raised. Note that this will require an additional call to the db.
210
+ # Will persist any changes you have made to the document. Silently fails on
211
+ # db update error. Use update! or pass in {:safe => true} to raise a
212
+ # Mongo::OperationError if that's what you want.
213
+ # If you want to raise the following errors also, pass in {:raise => true}
214
+ # * Raises Mongomatic::Exceptions::DocumentIsNew if document is new
215
+ # * Raises Mongomatic::Exceptions::DocumentNotValid if there are validation errors
216
+ # * Raises Mongomatic::Exceptions::DocumentWasRemoved if document has been removed
202
217
  def update(opts={},update_doc=@doc)
203
- return false if new? || removed? || !valid?
218
+ if opts[:raise] == true
219
+ raise Mongomatic::Exceptions::DocumentWasRemoved if removed?
220
+ raise Mongomatic::Exceptions::DocumentIsNew if new?
221
+ raise Mongomatic::Exceptions::DocumentNotValid unless valid?
222
+ else
223
+ return false if new? || removed? || !valid?
224
+ end
204
225
  do_callback(:before_update)
205
226
  do_callback(:before_insert_or_update)
206
227
  ret = self.class.collection.update({"_id" => @doc["_id"]}, update_doc, opts)
@@ -209,15 +230,28 @@ module Mongomatic
209
230
  ret
210
231
  end
211
232
 
212
- # Same as update(...) but will raise a Mongo::OperationError in case of any issues.
233
+ # Calls update(...) with {:safe => true} passed in as an option.
234
+ # * Raises Mongo::OperationError if there was a DB error on updating
235
+ # If you want to raise the following errors also, pass in {:raise => true}
236
+ # * Raises Mongomatic::Exceptions::DocumentIsNew if document is new
237
+ # * Raises Mongomatic::Exceptions::DocumentNotValid if there are validation errors
238
+ # * Raises Mongomatic::Exceptions::DocumentWasRemoved if document has been removed
213
239
  def update!(opts={},update_doc=@doc)
214
240
  update(opts.merge(:safe => true),update_doc)
215
241
  end
216
242
 
217
- # Remove this document from the collection. Silently fails on error, use remove!
218
- # if you want an exception raised.
243
+ # Remove this document from the collection. Silently fails on db error,
244
+ # use remove! or pass in {:safe => true} if you want an exception raised.
245
+ # If you want to raise the following errors also, pass in {:raise => true}
246
+ # * Raises Mongomatic::Exceptions::DocumentIsNew if document is new
247
+ # * Raises Mongomatic::Exceptions::DocumentWasRemoved if document has been already removed
219
248
  def remove(opts={})
220
- return false if new?
249
+ if opts[:raise] == true
250
+ raise Mongomatic::Exceptions::DocumentWasRemoved if removed?
251
+ raise Mongomatic::Exceptions::DocumentIsNew if new?
252
+ else
253
+ return false if new? || removed?
254
+ end
221
255
  do_callback(:before_remove)
222
256
  if ret = self.class.collection.remove({"_id" => @doc["_id"]})
223
257
  self.removed = true; freeze; ret
@@ -226,8 +260,11 @@ module Mongomatic
226
260
  ret
227
261
  end
228
262
 
229
- # Like remove(...) but raises Mongo::OperationError if MongoDB is unable to
230
- # remove the document.
263
+ # Calls remove(...) with {:safe => true} passed in as an option.
264
+ # * Raises Mongo::OperationError if there was a DB error on removing
265
+ # If you want to raise the following errors also, pass in {:raise => true}
266
+ # * Raises Mongomatic::Exceptions::DocumentIsNew if document is new
267
+ # * Raises Mongomatic::Exceptions::DocumentWasRemoved if document has been already removed
231
268
  def remove!(opts={})
232
269
  remove(opts.merge(:safe => true))
233
270
  end
@@ -0,0 +1,10 @@
1
+ module Mongomatic
2
+ module Exceptions
3
+ class Base < Exception; end
4
+
5
+ class DocumentNotNew < Base; end
6
+ class DocumentIsNew < Base; end
7
+ class DocumentWasRemoved < Base; end
8
+ class DocumentNotValid < Base; end
9
+ end
10
+ end
data/lib/mongomatic.rb CHANGED
@@ -29,6 +29,7 @@ module Mongomatic
29
29
  end
30
30
  end
31
31
 
32
+ require "#{File.dirname(__FILE__)}/mongomatic/exceptions"
32
33
  require "#{File.dirname(__FILE__)}/mongomatic/util"
33
34
  require "#{File.dirname(__FILE__)}/mongomatic/m_hash"
34
35
  require "#{File.dirname(__FILE__)}/mongomatic/cursor"
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class TestExceptions < Test::Unit::TestCase
4
+ def setup
5
+ Person.collection.drop
6
+ end
7
+
8
+ should "raise on updating new document" do
9
+ p1 = Person.new(:name => "Jordan")
10
+ assert_equal false, p1.update
11
+ assert_raise(Mongomatic::Exceptions::DocumentIsNew) { p1.update(:raise => true) }
12
+ end
13
+
14
+ should "raise on inserting invalid document" do
15
+ p1 = Person.new
16
+ assert_equal false, p1.insert
17
+ assert_raise(Mongomatic::Exceptions::DocumentNotValid) { p1.insert(:raise => true) }
18
+ end
19
+
20
+ should "raise on updating invalid document" do
21
+ p1 = Person.new(:name => "Ben")
22
+ assert p1.insert
23
+ p1["name"] = nil
24
+ assert_equal false, p1.update
25
+ assert_raise(Mongomatic::Exceptions::DocumentNotValid) { p1.update(:raise => true) }
26
+ end
27
+
28
+ should "raise on remove" do
29
+ p1 = Person.new(:name => "Ben")
30
+ assert p1.insert
31
+ assert p1.remove
32
+ assert_raise(Mongomatic::Exceptions::DocumentWasRemoved) { p1.remove(:raise => true) }
33
+ assert_raise(Mongomatic::Exceptions::DocumentWasRemoved) { p1.update(:raise => true) }
34
+ assert_raise(Mongomatic::Exceptions::DocumentWasRemoved) { p1.insert(:raise => true) }
35
+ end
36
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 1
9
- version: 0.6.1
8
+ - 2
9
+ version: 0.6.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -90,6 +90,7 @@ files:
90
90
  - lib/mongomatic/base.rb
91
91
  - lib/mongomatic/cursor.rb
92
92
  - lib/mongomatic/errors.rb
93
+ - lib/mongomatic/exceptions.rb
93
94
  - lib/mongomatic/expectations.rb
94
95
  - lib/mongomatic/expectations/be_reference.rb
95
96
  - lib/mongomatic/expectations/expected.rb
@@ -103,6 +104,7 @@ files:
103
104
  - LICENSE
104
105
  - README.rdoc
105
106
  - test/helper.rb
107
+ - test/test_exceptions.rb
106
108
  - test/test_modifiers.rb
107
109
  - test/test_mongomatic.rb
108
110
  has_rdoc: true
@@ -139,5 +141,6 @@ specification_version: 3
139
141
  summary: Mongomatic is a simple Ruby object mapper for Mongo
140
142
  test_files:
141
143
  - test/helper.rb
144
+ - test/test_exceptions.rb
142
145
  - test/test_modifiers.rb
143
146
  - test/test_mongomatic.rb