Skip to content

Commit

Permalink
Updating collections namespace rdoc to yard
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Mar 5, 2011
1 parent fde7aa3 commit a0be304
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
13 changes: 8 additions & 5 deletions lib/mongoid/collections/master.rb
@@ -1,6 +1,8 @@
# encoding: utf-8
module Mongoid #:nodoc:
module Collections #:nodoc:

# This class wraps the MongoDB master database.
class Master
include Mongoid::Collections::Retry

Expand All @@ -9,9 +11,8 @@ class Master
# All read and write operations should delegate to the master connection.
# These operations mimic the methods on a Mongo:Collection.
#
# Example:
#
# <tt>collection.save({ :name => "Al" })</tt>
# @example Proxy the driver save.
# collection.save({ :name => "Al" })
Operations::ALL.each do |name|
define_method(name) do |*args|
retry_on_connection_failure do
Expand All @@ -23,9 +24,11 @@ class Master
# Create the new database writer. Will create a collection from the
# master database.
#
# Example:
# @example Create a new wrapped master.
# Master.new(db, "testing")
#
# <tt>Master.new(master, "mongoid_people")</tt>
# @param [ Mongo::DB ] master The master database.
# @param [ String ] name The name of the database.
def initialize(master, name)
@collection = master.collection(name)
end
Expand Down
10 changes: 10 additions & 0 deletions lib/mongoid/collections/retry.rb
@@ -1,7 +1,10 @@
# encoding: utf-8
module Mongoid #:nodoc:
module Collections #:nodoc:

# Provides behaviour for retrying commands on connection failure.
module Retry

# Retries command on connection failures.
#
# This is useful when using replica sets. When a primary server wents
Expand All @@ -13,6 +16,13 @@ module Retry
# no attempt will be made, immediately raising connection failure.
# Otherwise it will attempt to make the specified number of retries
# and then raising the exception to clients.
#
# @example Retry the command.
# retry_on_connection_failure do
# collection.send(name, *args)
# end
#
# @since 2.0.0
def retry_on_connection_failure
retries = 0
begin
Expand Down
29 changes: 20 additions & 9 deletions lib/mongoid/collections/slaves.rb
@@ -1,6 +1,8 @@
# encoding: utf-8
module Mongoid #:nodoc:
module Collections #:nodoc:

# This class wraps the MongoDB slaves databases.
class Slaves
include Mongoid::Collections::Retry

Expand All @@ -9,9 +11,8 @@ class Slaves
# All read operations should delegate to the slave connections.
# These operations mimic the methods on a Mongo:Collection.
#
# Example:
#
# <tt>collection.save({ :name => "Al" })</tt>
# @example Proxy the driver save.
# collection.save({ :name => "Al" })
Operations::READ.each do |name|
define_method(name) do |*args|
retry_on_connection_failure do
Expand All @@ -22,28 +23,38 @@ class Slaves

# Is the collection of slaves empty or not?
#
# Return:
# @example Is the collection empty?
# slaves.empty?
#
# True is the iterator is not set, false if not.
# @return [ true, false ] If the iterator is set or not.
def empty?
@iterator.nil?
iterator.nil?
end

# Create the new database reader. Will create a collection from the
# slave databases and cycle through them on each read.
#
# Example:
# @example Create the slaves.
# Reader.new(slaves, "mongoid_people")
#
# <tt>Reader.new(slaves, "mongoid_people")</tt>
# @param [ Array<Mongo::DB> ] slaves The slave databases.
# @param [ String ] name The database name.
def initialize(slaves, name)
unless slaves.blank?
@iterator = CyclicIterator.new(slaves.collect { |db| db.collection(name) })
end
end

protected

# Get the next database in the round-robin.
#
# @example Get the next database.
# slaves.collection
#
# @return [ Mongo::DB ] The next slave database to read from.
def collection
@iterator.next
iterator.next
end
end
end
Expand Down

0 comments on commit a0be304

Please sign in to comment.