Permalink
Browse files

Added atomic update example.

  • Loading branch information...
jnunemaker committed May 20, 2012
1 parent d60b6d4 commit f70b91c2afb036257041b8c5b7e55b37faab77bc
Showing with 36 additions and 0 deletions.
  1. +36 −0 examples/atomic_updates.rb
View
@@ -0,0 +1,36 @@
+require 'pathname'
+require 'rubygems'
+require 'bundler'
+
+Bundler.require(:default)
+
+root_path = Pathname(__FILE__).dirname.join('..').expand_path
+lib_path = root_path.join('lib')
+$:.unshift(lib_path)
+
+require 'toy/mongo'
+
+class User
+ include Toy::Mongo
+ include Toy::Mongo::AtomicUpdates
+
+ adapter :mongo_atomic, Mongo::Connection.new.db('adapter')['testing']
+
+ attribute :name, String
+ attribute :bio, String
+end
+
+user = User.create(:name => 'John', :bio => 'Awesome!')
+puts user.name # John
+puts user.bio # Awesome!
+
+# simulate another process updating only bio
+user.adapter.client.update({:_id => user.id}, '$set' => {:bio => "Changed!"})
+
+user.name = 'Nunes'
+user.save # save performs update with $set's rather than full doc save
+
+user.reload
+
+puts user.name # Nunes
+puts user.bio # Changed!

0 comments on commit f70b91c

Please sign in to comment.