/
mongo_atomic.rb
36 lines (26 loc) · 949 Bytes
/
mongo_atomic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'rubygems'
require 'pathname'
root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path = root_path.join('lib')
$:.unshift(lib_path)
require 'adapter/mongo_atomic'
key = BSON::ObjectId.new
full_doc = {'a' => 'c', 'b' => 'd'}
partial_doc = {'a' => 'z'}
client = Mongo::MongoClient.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')
# full doc must always be written with :mongo adapter
puts 'Should be {"a"=>"z"}: ' + doc.inspect
atomic_adapter.write(key, full_doc)
atomic_adapter.write(key, partial_doc)
doc = atomic_adapter.read(key)
doc.delete('_id')
# partial updates can be written with atomic adapter as $set is used
puts 'Should be {"a"=>"z", "b"=>"d"}: ' + doc.inspect