mongodb-mongo 0.7 → 0.8

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/README.rdoc CHANGED
@@ -321,6 +321,8 @@ Adrian Madrid, aemadrid@gmail.com
321
321
  * Gem support.
322
322
  * Many other code suggestions and improvements.
323
323
 
324
+ Aman Gupta, aman@tmm1.net
325
+ * Collection#save
324
326
 
325
327
  = License
326
328
 
@@ -73,6 +73,15 @@ module XGen
73
73
  cursor.next_object # don't need to explicitly close b/c of limit
74
74
  end
75
75
 
76
+ # Save an updated +object+ to the collection, or insert it if it doesn't exist already.
77
+ def save(object)
78
+ if id = object[:_id] || object['_id']
79
+ modify({:_id => id}, object)
80
+ else
81
+ insert(object)
82
+ end
83
+ end
84
+
76
85
  # Insert +objects+, which are hashes. "<<" is aliased to this method.
77
86
  # Returns either the single inserted object or a new array containing
78
87
  # +objects+. The object(s) may have been modified by the database's PK
data/lib/mongo/db.rb CHANGED
@@ -264,6 +264,27 @@ module XGen
264
264
  error != nil
265
265
  end
266
266
 
267
+ # Get the most recent error to have occured on this database
268
+ #
269
+ # Only returns errors that have occured since the last call to
270
+ # DB#reset_error_history - returns +nil+ if there is no such error.
271
+ def previous_error
272
+ error = db_command(:getpreverror => 1)
273
+ if error["err"]
274
+ error
275
+ else
276
+ nil
277
+ end
278
+ end
279
+
280
+ # Reset the error history of this database
281
+ #
282
+ # Calls to DB#previous_error will only return errors that have occurred
283
+ # since the most recent call to this method.
284
+ def reset_error_history
285
+ db_command(:reseterror => 1)
286
+ end
287
+
267
288
  # Returns true if this database is a master (or is not paired with any
268
289
  # other database), false if it is a slave.
269
290
  def master?
@@ -21,9 +21,13 @@
21
21
  class OrderedHash < Hash
22
22
 
23
23
  def ==(other)
24
- !other.nil? &&
25
- keys == other.keys &&
26
- values == other.values
24
+ begin
25
+ !other.nil? &&
26
+ keys == other.keys &&
27
+ values == other.values
28
+ rescue
29
+ false
30
+ end
27
31
  end
28
32
 
29
33
  # We only need the body of this class if the RUBY_VERSION is before 1.9
@@ -79,7 +79,7 @@ TEST_FILES = ['tests/mongo-qa/_common.rb',
79
79
 
80
80
  Gem::Specification.new do |s|
81
81
  s.name = 'mongo'
82
- s.version = '0.7'
82
+ s.version = '0.8'
83
83
  s.platform = Gem::Platform::RUBY
84
84
  s.summary = 'Ruby driver for the 10gen Mongo DB'
85
85
  s.description = 'A Ruby driver for the 10gen Mongo DB. For more information about Mongo, see http://www.mongodb.org.'
data/tests/test_db.rb CHANGED
@@ -122,15 +122,33 @@ class DBTest < Test::Unit::TestCase
122
122
  end
123
123
 
124
124
  def test_error
125
- doc = @@db.send(:db_command, :forceerror => 1)
125
+ @@db.reset_error_history
126
+ assert_nil @@db.error
127
+ assert !@@db.error?
128
+ assert_nil @@db.previous_error
129
+
130
+ @@db.send(:db_command, :forceerror => 1)
126
131
  assert @@db.error?
127
- err = @@db.error
128
- assert_match /forced error/, err
132
+ assert_not_nil @@db.error
133
+ assert_not_nil @@db.previous_error
129
134
 
130
- # ask again
135
+ @@db.send(:db_command, :forceerror => 1)
131
136
  assert @@db.error?
132
- err2 = @@db.error
133
- assert_equal err, err2
137
+ assert @@db.error
138
+ prev_error = @@db.previous_error
139
+ assert_equal 1, prev_error['nPrev']
140
+ assert_equal prev_error["err"], @@db.error
141
+
142
+ @@db.collection('test').find_first
143
+ assert_nil @@db.error
144
+ assert !@@db.error?
145
+ assert @@db.previous_error
146
+ assert_equal 2, @@db.previous_error['nPrev']
147
+
148
+ @@db.reset_error_history
149
+ assert_nil @@db.error
150
+ assert !@@db.error?
151
+ assert_nil @@db.previous_error
134
152
  end
135
153
 
136
154
  def test_text_port_number
data/tests/test_db_api.rb CHANGED
@@ -561,6 +561,30 @@ class DBAPITest < Test::Unit::TestCase
561
561
  assert_equal nil, @@db.dereference(DBRef.new("test", nil))
562
562
  end
563
563
 
564
+ def test_save
565
+ @@coll.clear
566
+
567
+ a = {"hello" => "world"}
568
+
569
+ @@coll.save(a)
570
+ assert_equal 1, @@coll.count
571
+
572
+ @@coll.save(@@coll.find_first)
573
+ assert_equal 1, @@coll.count
574
+
575
+ assert_equal "world", @@coll.find_first()["hello"]
576
+
577
+ doc = @@coll.find_first
578
+ doc["hello"] = "mike"
579
+ @@coll.save(doc)
580
+ assert_equal 1, @@coll.count
581
+
582
+ assert_equal "mike", @@coll.find_first()["hello"]
583
+
584
+ @@coll.save(a)
585
+ assert_equal 2, @@coll.count
586
+ end
587
+
564
588
  # TODO this test fails with error message "Undefed Before end of object"
565
589
  # That is a database error. The undefined type may go away.
566
590
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.7"
4
+ version: "0.8"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Menard
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-04-08 00:00:00 -07:00
13
+ date: 2009-05-16 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16