Permalink
Browse files

Adding Criteria#cache

  • Loading branch information...
1 parent aba4366 commit 101882e2074b0df98111505e4225e54f74215f27 @durran durran committed Feb 10, 2010
Showing with 24 additions and 0 deletions.
  1. +12 −0 lib/mongoid/criterion/optional.rb
  2. +12 −0 spec/unit/mongoid/criterion/optional_spec.rb
View
12 lib/mongoid/criterion/optional.rb
@@ -2,6 +2,18 @@
module Mongoid #:nodoc:
module Criterion #:nodoc:
module Optional
+ # Tells the criteria that the cursor that gets returned needs to be
+ # cached. This is so multiple iterations don't hit the database multiple
+ # times, however this is not advisable when working with large data sets
+ # as the entire results will get stored in memory.
+ #
+ # Example:
+ #
+ # <tt>criteria.cache</tt>
+ def cache
+ @options.merge!(:cache => true); self
+ end
+
# Flags the criteria to execute against a read-only slave in the pool
# instead of master.
#
View
12 spec/unit/mongoid/criterion/optional_spec.rb
@@ -7,6 +7,18 @@
@canvas_criteria = Mongoid::Criteria.new(Canvas)
end
+ describe "#cache" do
+
+ it "sets the cache option on the criteria" do
+ @criteria.cache
+ @criteria.options[:cache].should be_true
+ end
+
+ it "returns self" do
+ @criteria.cache.should == @criteria
+ end
+ end
+
describe "#enslave" do
it "sets the enslaved option on the criteria" do

0 comments on commit 101882e

Please sign in to comment.