Permalink
Browse files

Tweaked mongo atomic example to show difference verse plain old mongo…

… adapter.
  • Loading branch information...
1 parent d1541bd commit f337b768f8e6a3339804d6d08e58137a8d40e320 @jnunemaker committed Jun 25, 2012
Showing with 22 additions and 8 deletions.
  1. +22 −8 examples/mongo_atomic.rb
View
@@ -7,16 +7,30 @@
require 'adapter/mongo_atomic'
-client = Mongo::Connection.new.db('adapter')['testing']
-adapter = Adapter[:mongo_atomic].new(client)
+key = BSON::ObjectId.new
+full_doc = {'a' => 'c', 'b' => 'd'}
+partial_doc = {'a' => 'z'}
+client = Mongo::Connection.new.db('adapter')['testing']
+adapter = Adapter[:mongo].new(client)
+atomic_adapter = Adapter[:mongo_atomic].new(client)
+
adapter.clear
+atomic_adapter.clear
+
+adapter.write(key, full_doc)
+adapter.write(key, partial_doc)
+
+doc = adapter.read(key)
+doc.delete('_id')
-oid = BSON::ObjectId.new
+# full doc must always be written with :mongo adapter
+puts 'Should be {"a"=>"z"}: ' + doc.inspect
-adapter.write(oid, {'a' => 'c', 'b' => 'd'})
-adapter.write(oid, {'a' => 'z'})
+atomic_adapter.write(key, full_doc)
+atomic_adapter.write(key, partial_doc)
-doc = adapter.read(oid)
+doc = atomic_adapter.read(key)
+doc.delete('_id')
-puts 'Should be "z": ' + doc['a']
-puts 'Should be "d": ' + doc['b']
+# partial updates can be written with atomic adapter as $set is used
+puts 'Should be {"a"=>"z", "b"=>"d"}: ' + doc.inspect

0 comments on commit f337b76

Please sign in to comment.