Skip to content

Commit

Permalink
Removing memoization of cursor in criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Feb 8, 2010
1 parent 2e27bc0 commit e324edb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
6 changes: 2 additions & 4 deletions lib/mongoid/criteria.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def ==(other)
when Criteria when Criteria
self.selector == other.selector && self.options == other.options self.selector == other.selector && self.options == other.options
when Enumerable when Enumerable
@collection ||= execute return (execute.entries == other)
return (@collection.entries == other)
else else
return false return false
end end
Expand Down Expand Up @@ -119,9 +118,8 @@ def fuse(criteria_conditions = {})
# #
# <tt>criteria.each { |doc| p doc }</tt> # <tt>criteria.each { |doc| p doc }</tt>
def each(&block) def each(&block)
@collection ||= execute
if block_given? if block_given?
@collection.each { |doc| yield doc } execute.each { |doc| yield doc }
end end
self self
end end
Expand Down
28 changes: 28 additions & 0 deletions spec/integration/mongoid/criteria_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@


describe Mongoid::Criteria do describe Mongoid::Criteria do


before do
Person.delete_all
end

after do
Person.delete_all
end

describe "#excludes" do describe "#excludes" do


before do before do
Expand Down Expand Up @@ -30,6 +38,26 @@


end end


describe "#execute" do

context "when reiterating" do

before do
@person = Person.create(:title => "Sir", :age => 100, :aliases => ["D", "Durran"], :ssn => "666666666")
end

after do
Person.delete_all
end

it "executes the query again" do
criteria = Person.all
criteria.size.should == 1
criteria.should_not be_empty
end
end
end

describe "#max" do describe "#max" do


before do before do
Expand Down
60 changes: 14 additions & 46 deletions spec/unit/mongoid/criteria_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
before do before do
@collection = mock @collection = mock
@cursor = stub(:count => 1) @cursor = stub(:count => 1)
@cursor.expects(:each).yields(@sir) @cursor.expects(:each).at_least_once.yields(@sir)
Person.expects(:collection).returns(@collection) Person.expects(:collection).at_least_once.returns(@collection)
@collection.expects(:find).returns(@cursor) @collection.expects(:find).at_least_once.returns(@cursor)
end end


it "executes the criteria and concats the results" do it "executes the criteria and concats the results" do
Expand All @@ -31,29 +31,24 @@


end end


context "when the criteria has been executed" do

before do
@criteria.instance_variable_set(:@collection, [ @sir, @madam ])
end

it "concats the results" do
results = @criteria + [ @canvas ]
results.should == [ @sir, @madam, @canvas ]
end

end

context "when the other is a criteria" do context "when the other is a criteria" do


before do before do
@criteria.instance_variable_set(:@collection, [ @sir, @madam ]) @collection = mock
@canvas_criteria.instance_variable_set(:@collection, [ @canvas ]) @canvas_collection = mock
@cursor = stub(:count => 1)
@canvas_cursor = stub(:count => 1)
@cursor.expects(:each).at_least_once.yields(@sir)
@canvas_cursor.expects(:each).at_least_once.yields(@canvas)
Person.expects(:collection).at_least_once.returns(@collection)
@collection.expects(:find).at_least_once.returns(@cursor)
Canvas.expects(:collection).at_least_once.returns(@canvas_collection)
@canvas_collection.expects(:find).at_least_once.returns(@canvas_cursor)
end end


it "concats the results" do it "concats the results" do
results = @criteria + @canvas_criteria results = @criteria + @canvas_criteria
results.should == [ @sir, @madam, @canvas ] results.should == [ @sir, @canvas ]
end end


end end
Expand Down Expand Up @@ -84,33 +79,6 @@


end end


context "when the criteria has been executed" do

before do
@criteria.instance_variable_set(:@collection, [@sir, @sir, @madam])
end

it "returns the difference" do
results = @criteria - [ @sir ]
results.should == [ @madam ]
end

end

context "when the other is a criteria" do

before do
@criteria.instance_variable_set(:@collection, [@sir, @sir, @madam])
@canvas_criteria.instance_variable_set(:@collection, [@sir])
end

it "returns the difference" do
results = @criteria - @canvas_criteria
results.should == [ @madam ]
end

end

end end


describe "#[]" do describe "#[]" do
Expand Down

0 comments on commit e324edb

Please sign in to comment.