Browse files

Add readme examples to an examples folder, make sure the readme examp…

…le really works
  • Loading branch information...
1 parent 5d88f68 commit 9acc283e9ea7700a08c2488fe76414f1862e7a44 @PlasticLizard PlasticLizard committed Jul 13, 2011
Showing with 82 additions and 16 deletions.
  1. +24 −15 README.rdoc
  2. +57 −0 examples/readme.rb
  3. +1 −1 lib/em-mongo/exceptions.rb
View
39 README.rdoc
@@ -9,17 +9,20 @@ For operations that require IO, em-mongo always returns an EventMachine deferrab
== Some examples
+ #this file can be found in the examples directory.
+ # bundle exec examples/readme.rb
+
#insert a few records, then read some back using Collection#find
require 'em-mongo'
require 'eventmachine'
EM.run do
- db = EM::Mongo::Connection.new(:host => 'localhost').db('my_database')
+ db = EM::Mongo::Connection.new('localhost').db('my_database')
collection = db.collection('my_collection')
EM.next_tick do
(1..10).each do |i|
- collection.insert { :revolution => i }
+ collection.insert( { :revolution => i } )
end
#find returns an EM::Mongo::Cursor
@@ -35,7 +38,7 @@ For operations that require IO, em-mongo always returns an EventMachine deferrab
#synchronous version of the method from
#the mongo-ruby-driver
resp.callback do |documents|
- puts documents.inspect
+ puts "I just got #{documents.length} documents! I'm really cool!"
end
#when em-mongo IO methods fail, they
@@ -44,22 +47,28 @@ For operations that require IO, em-mongo always returns an EventMachine deferrab
resp.errback do |err|
raise *err
end
- end
- #iterate though each result in a query
- collection.find( :revolution => { "$gt" => 8 } ).limit(1).each do |doc|
- puts doc.inspect
- end
+ #iterate though each result in a query
+ collection.find( :revolution => { "$gt" => 5 } ).limit(1).skip(1).each do |doc|
+ #unlike the mongo-ruby-driver, each returns null at the end of the cursor
+ if doc
+ puts "Revolution ##{doc['revolution']}"
+ end
+ end
+
+ #add an index
+ collection.create_index [[:revolution, -1]]
- #add an index
- collection.create_index [[:revolution, -1]]
+ #insert a document and ensure it gets written
+ save_resp = collection.safe_save( { :hi => "there" }, :last_error_params => {:fsync=>true} )
+ save_resp.callback { puts "Hi is there, let us give thanks" }
+ save_resp.errback { |err| puts "AAAAAAAAAAAAAAAARGH! Oh why! WHY!?!?!" }
- #insert a document and ensure it gets written
- save_resp = collection.safe_save( { :hi => "there" }, :last_error_params => {:fsync=>true} )
- save_resp.callback { puts "Hi is there, let us give thanks" }
- save_resp.errback { |err| puts "AAAAAAAAAAAAAAAARGH! Oh why! WHY!?!?!" }
+ collection.drop
- EM.stop
+ EM.add_periodic_timer(1) { EM.stop }
+
+ end
end
View
57 examples/readme.rb
@@ -0,0 +1,57 @@
+require 'em-mongo'
+require 'eventmachine'
+
+EM.run do
+ db = EM::Mongo::Connection.new('localhost').db('my_database')
+ collection = db.collection('my_collection')
+ EM.next_tick do
+ (1..10).each do |i|
+ collection.insert( { :revolution => i } )
+ end
+
+ #find returns an EM::Mongo::Cursor
+ cursor = collection.find
+
+ #most cursor methods return an EM::Mongo::RequestResponse,
+ #which is an EventMachine::Deferrable
+ resp = cursor.to_a
+
+ #when em-mongo IO methods succeed, they
+ #will always call back with the return
+ #value you would have expected from the
+ #synchronous version of the method from
+ #the mongo-ruby-driver
+ resp.callback do |documents|
+ puts "I just got #{documents.length} documents! I'm really cool!"
+ end
+
+ #when em-mongo IO methods fail, they
+ #errback with an array in the form
+ #[ErrorClass, "error message"]
+ resp.errback do |err|
+ raise *err
+ end
+
+ #iterate though each result in a query
+ collection.find( :revolution => { "$gt" => 5 } ).limit(1).skip(1).each do |doc|
+ #unlike the mongo-ruby-driver, each returns null at the end of the cursor
+ if doc
+ puts "Revolution ##{doc['revolution']}"
+ end
+ end
+
+ #add an index
+ collection.create_index [[:revolution, -1]]
+
+ #insert a document and ensure it gets written
+ save_resp = collection.safe_save( { :hi => "there" }, :last_error_params => {:fsync=>true} )
+ save_resp.callback { puts "Hi is there, let us give thanks" }
+ save_resp.errback { |err| puts "AAAAAAAAAAAAAAAARGH! Oh why! WHY!?!?!" }
+
+ collection.drop
+
+ EM.add_periodic_timer(1) { EM.stop }
+
+ end
+
+end
View
2 lib/em-mongo/exceptions.rb
@@ -1,4 +1,4 @@
-
+require "timeout"
# encoding: UTF-8
#

0 comments on commit 9acc283

Please sign in to comment.