Navigation Menu

Skip to content

Commit

Permalink
Updating mongoid colection with reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 6, 2010
1 parent 3da5a74 commit a37c513
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
9 changes: 3 additions & 6 deletions lib/mongoid.rb
Expand Up @@ -68,15 +68,12 @@ class << self

delegate \
:allow_dynamic_fields,
:allow_dynamic_fields=,
:database,
:database=,
:master,
:persist_in_safe_mode,
:persist_in_safe_mode=,
:raise_not_found_error,
:raise_not_found_error=,
:temp_collection_size,
:temp_collection_size=, :to => :configure
:slaves,
:temp_collection_size, :to => :configure

def configure
config = Config.instance
Expand Down
8 changes: 5 additions & 3 deletions lib/mongoid/collection.rb
Expand Up @@ -6,16 +6,18 @@
module Mongoid #:nodoc
class Collection

attr_reader :master, :name, :slaves

# Initialize a new Mongoid::Collection, setting up the master, slave, and
# name attributes. Masters will be used for writes, slaves for reads.
#
# Example:
#
# <tt>Mongoid::Collection.new(masters, slaves, "test")</tt>
def initialize(name)
# Get all the master db -> Mongoid.master
# Get all the slave dbs -> Mongoid.slaves
@name = name
@master, @slaves, @name = Mongoid.master, Mongoid.slaves, name
@writer = Collections::Writer.new(@master, name)
@reader = Collections::Reader.new((@slaves || [ @master ]), name)
end
end
end
21 changes: 9 additions & 12 deletions lib/mongoid/collections/writer.rb
Expand Up @@ -4,18 +4,6 @@ module Collections #:nodoc:
class Writer
attr_reader :collection

delegate \
:<<,
:create_index,
:drop,
:drop_index,
:drop_indexes,
:insert,
:remove,
:rename,
:save,
:update, :to => :collection

# Create the new database writer. Will create a collection from the
# master database.
#
Expand All @@ -25,6 +13,15 @@ class Writer
def initialize(master_db, name)
@collection = master_db.collection(name)
end

# Send every method call to the master collection.
#
# Example:
#
# <tt>writer.find({})</tt>
def method_missing(name, *args, &block)
@collection.send(name, *args, &block)
end
end
end
end
5 changes: 1 addition & 4 deletions spec/spec_helper.rb
Expand Up @@ -15,10 +15,7 @@
Mongoid.configure do |config|
name = "mongoid_test"
config.database = Mongo::Connection.new.db(name)
# config.masters = [
# Mongo::Connection.new(host, port).db(name),
# Mongo::Connection.new(host, port).db(name)
# ]
# config.master = Mongo::Connection.new(host, port).db(name)
# config.slaves = [
# Mongo::Connection.new(host, port, :slave_ok => true).db(name),
# Mongo::Connection.new(host, port, :slave_ok => true).db(name)
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/mongoid/attributes_spec.rb
Expand Up @@ -118,7 +118,7 @@
describe "#method_missing" do

before do
Mongoid.allow_dynamic_fields = true
Mongoid.configure.allow_dynamic_fields = true
@attributes = {
:testing => "Testing"
}
Expand Down Expand Up @@ -155,7 +155,7 @@
context "when allowing dynamic fields" do

before do
Mongoid.allow_dynamic_fields = true
Mongoid.configure.allow_dynamic_fields = true
@person = Person.new(@attributes)
end

Expand All @@ -180,15 +180,15 @@
context "when not allowing dynamic fields" do

before do
Mongoid.allow_dynamic_fields = false
Mongoid.configure.allow_dynamic_fields = false
Person.fields.delete(:nofieldstring)
@attributes = {
:nofieldstring => "Testing"
}
end

after do
Mongoid.allow_dynamic_fields = true
Mongoid.configure.allow_dynamic_fields = true
end

it "raises an error" do
Expand Down
32 changes: 27 additions & 5 deletions spec/unit/mongoid/collection_spec.rb
Expand Up @@ -20,15 +20,37 @@

describe "#initialize" do

let(:collection) do
Mongoid::Collection.new(master, slaves, "mongoid_test")
before do
Mongoid.expects(:master).returns(master)
end

it "sets the master db"
context "when slaves exist" do

it "sets the slave dbs"
before do
Mongoid.expects(:slaves).returns(slaves)
Mongoid::Collections::Writer.expects(:new).with(master, "mongoid_test")
end

it "sets the reader with slaves" do
Mongoid::Collections::Reader.expects(:new).with(slaves, "mongoid_test")
Mongoid::Collection.new("mongoid_test")
end

end

context "when slaves do not exist" do

before do
Mongoid.expects(:slaves).returns(nil)
Mongoid::Collections::Writer.expects(:new).with(master, "mongoid_test")
end

it "sets the reader to an array with the master" do
Mongoid::Collections::Reader.expects(:new).with([ master ], "mongoid_test")
Mongoid::Collection.new("mongoid_test")
end
end

it "sets the name"
end

end

0 comments on commit a37c513

Please sign in to comment.