Permalink
Browse files

Adding Criteria#cached?

  • Loading branch information...
1 parent 101882e commit 256737ea48a3e323b9b9e5ef5f911c193f9f481b @durran durran committed Feb 10, 2010
Showing with 41 additions and 0 deletions.
  1. +10 −0 lib/mongoid/criterion/optional.rb
  2. +31 −0 spec/unit/mongoid/criterion/optional_spec.rb
@@ -14,6 +14,16 @@ def cache
@options.merge!(:cache => true); self
end
+ # Will return true if the cache option has been set.
+ #
+ # Example:
+ #
+ # <tt>criteria.cached?</tt>
+ def cached?
+ @cached ||= @options.delete(:cache)
+ @cached == true
+ end
+
# Flags the criteria to execute against a read-only slave in the pool
# instead of master.
#
@@ -19,6 +19,37 @@
end
end
+ describe "#cached?" do
+
+ context "when the criteria has a cache option" do
+
+ before do
+ @criteria.cache
+ end
+
+ it "returns true" do
+ @criteria.cached?.should be_true
+ end
+
+ it "removes cache from the options" do
+ @criteria.cached?
+ @criteria.options[:cache].should be_nil
+ end
+
+ it "sets the cache instance variable" do
+ @criteria.cached?
+ @criteria.instance_variable_get(:@cached).should be_true
+ end
+ end
+
+ context "when the criteria has no cache option" do
+
+ it "returns false" do
+ @criteria.cached?.should be_false
+ end
+ end
+ end
+
describe "#enslave" do
it "sets the enslaved option on the criteria" do

0 comments on commit 256737e

Please sign in to comment.