Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Passing class through to the cursor and collection

  • Loading branch information...
commit 922edede53e38e4d816cd796255739cc8a5e99c9 1 parent ead0be2
@durran durran authored
View
6 lib/mongoid/collection.rb
@@ -59,7 +59,7 @@ def directed
#
# <tt>collection.find({ :test => "value" })</tt>
def find(selector = {}, options = {})
- cursor = Mongoid::Cursor.new(self, directed.find(selector, options))
+ cursor = Mongoid::Cursor.new(@klass, self, directed.find(selector, options))
if block_given?
yield cursor; cursor.close
else
@@ -73,8 +73,8 @@ def find(selector = {}, options = {})
# Example:
#
# <tt>Mongoid::Collection.new(masters, slaves, "test")</tt>
- def initialize(name)
- @name, @counter = name, 0
+ def initialize(klass, name)
+ @klass, @name, @counter = klass, name, 0
end
# Return the object responsible for reading documents from the database.
View
6 lib/mongoid/contexts/mongo.rb
@@ -79,7 +79,7 @@ def group
true
).collect do |docs|
docs["group"] = docs["group"].collect do |attrs|
- Mongoid::Factory.build(attrs)
+ Mongoid::Factory.build(@klass, attrs)
end
docs
end
@@ -115,7 +115,7 @@ def last
sorting = [[:_id, :asc]] unless sorting
opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
attributes = @klass.collection.find_one(@selector, opts)
- attributes ? Mongoid::Factory.build(attributes) : nil
+ attributes ? Mongoid::Factory.build(@klass, attributes) : nil
end
MAX_REDUCE = "function(obj, prev) { if (prev.max == 'start') { prev.max = obj.[field]; } " +
@@ -169,7 +169,7 @@ def min(field)
# The first document in the collection.
def one
attributes = @klass.collection.find_one(@selector, process_options)
- attributes ? Mongoid::Factory.build(attributes) : nil
+ attributes ? Mongoid::Factory.build(@klass, attributes) : nil
end
alias :first :one
View
12 lib/mongoid/cursor.rb
@@ -42,7 +42,7 @@ class Cursor
# <tt>cursor.each { |doc| p doc.title }</tt>
def each
@cursor.each do |document|
- yield Mongoid::Factory.build(document)
+ yield Mongoid::Factory.build(@klass, document)
end
end
@@ -55,9 +55,9 @@ def each
#
# Example:
#
- # <tt>Mongoid::Cursor.new(collection, cursor)</tt>
- def initialize(collection, cursor)
- @collection, @cursor = collection, cursor
+ # <tt>Mongoid::Cursor.new(Person, cursor)</tt>
+ def initialize(klass, collection, cursor)
+ @klass, @collection, @cursor = klass, collection, cursor
end
# Return the next document in the cursor. Will instantiate a new Mongoid
@@ -67,7 +67,7 @@ def initialize(collection, cursor)
#
# <tt>cursor.next_document</tt>
def next_document
- Mongoid::Factory.build(@cursor.next_document)
+ Mongoid::Factory.build(@klass, @cursor.next_document)
end
# Returns an array of all the documents in the cursor.
@@ -76,7 +76,7 @@ def next_document
#
# <tt>cursor.to_a</tt>
def to_a
- @cursor.to_a.collect { |attrs| Mongoid::Factory.build(attrs) }
+ @cursor.to_a.collect { |attrs| Mongoid::Factory.build(@klass, attrs) }
end
end
end
View
4 lib/mongoid/document.rb
@@ -33,7 +33,7 @@ module ClassMethods
# Returns: <tt>Mongo::Collection</tt>
def collection
raise Errors::InvalidCollection.new(self) if embedded
- self._collection ||= Mongoid::Collection.new(self.collection_name)
+ self._collection ||= Mongoid::Collection.new(self, self.collection_name)
add_indexes; self._collection
end
@@ -94,7 +94,7 @@ def key(*fields)
# <tt>Person.store_in :populdation</tt>
def store_in(name)
self.collection_name = name.to_s
- self._collection = Mongoid::Collection.new(name.to_s)
+ self._collection = Mongoid::Collection.new(self, name.to_s)
end
# Returns all types to query for when using this class as the base.
View
6 lib/mongoid/factory.rb
@@ -9,9 +9,11 @@ class Factory #:nodoc:
#
# Options:
#
+ # klass: The class to instantiate from if _type is not present.
# attributes: The +Document+ attributes.
- def self.build(attributes)
- attributes["_type"].constantize.instantiate(attributes)
+ def self.build(klass, attrs)
+ type = attrs["_type"]
+ type ? type.constantize.instantiate(attrs) : klass.instantiate(attrs)
end
end
end
View
4 spec/unit/mongoid/collection_spec.rb
@@ -11,7 +11,7 @@
end
let(:collection) do
- Mongoid::Collection.new("people")
+ Mongoid::Collection.new(Person, "people")
end
before do
@@ -93,7 +93,7 @@
before do
@cursor = stub.quacks_like(Mongoid::Cursor.allocate)
master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor)
- Mongoid::Cursor.expects(:new).with(collection, @mongo_cursor).returns(@cursor)
+ Mongoid::Cursor.expects(:new).with(Person, collection, @mongo_cursor).returns(@cursor)
end
it "finds are returns a cursor" do
View
2  spec/unit/mongoid/cursor_spec.rb
@@ -11,7 +11,7 @@
end
let(:cursor) do
- Mongoid::Cursor.new(collection, proxy)
+ Mongoid::Cursor.new(Person, collection, proxy)
end
(Mongoid::Cursor::OPERATIONS - [ :timeout ]).each do |name|
View
9 spec/unit/mongoid/document_spec.rb
@@ -6,8 +6,8 @@
@database = mock
@collection = stub(:name => "people")
@canvas_collection = stub(:name => "canvases")
- Mongoid::Collection.stubs(:new).with("people").returns(@collection)
- Mongoid::Collection.stubs(:new).with("canvases").returns(@canvas_collection)
+ Mongoid::Collection.stubs(:new).with(Person, "people").returns(@collection)
+ Mongoid::Collection.stubs(:new).with(Canvas, "canvases").returns(@canvas_collection)
@collection.stubs(:create_index).with(:_type, false)
@canvas_collection.stubs(:create_index).with(:_type, false)
end
@@ -552,7 +552,7 @@
context "on a parent class" do
it "sets the collection name and collection for the document" do
- Mongoid::Collection.expects(:new).with("population").returns(@collection)
+ Mongoid::Collection.expects(:new).with(Patient, "population").returns(@collection)
Patient.store_in :population
Patient.collection_name.should == "population"
end
@@ -562,11 +562,12 @@
context "on a subclass" do
after do
+ Mongoid::Collection.expects(:new).with(Firefox, "canvases")
Firefox.store_in :canvases
end
it "changes the collection name for the entire hierarchy" do
- Mongoid::Collection.expects(:new).with("browsers").returns(@collection)
+ Mongoid::Collection.expects(:new).with(Firefox, "browsers").returns(@collection)
Firefox.store_in :browsers
Canvas.collection_name.should == "browsers"
end
View
25 spec/unit/mongoid/factory_spec.rb
@@ -4,13 +4,28 @@
describe ".build" do
- before do
- @attributes = { "_type" => "Person", "title" => "Sir" }
+ context "when the _type attribute is present" do
+
+ before do
+ @attributes = { "_type" => "Person", "title" => "Sir" }
+ end
+
+ it "instantiates based on the type" do
+ person = Mongoid::Factory.build(Person, @attributes)
+ person.title.should == "Sir"
+ end
end
- it "instantiates based on the type" do
- person = Mongoid::Factory.build(@attributes)
- person.title.should == "Sir"
+ context "when _type is not preset" do
+
+ before do
+ @attributes = { "title" => "Sir" }
+ end
+
+ it "instantiates based on the type" do
+ person = Mongoid::Factory.build(Person, @attributes)
+ person.title.should == "Sir"
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.