Sha256: c632f594c2637d6400339c15f16ff67744c104e63b49e06d4c895246406d47bb

Contents?: true

Size: 1.13 KB

Versions: 9

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:

  # Adds support for caching queries at the class level.
  module Extras
    extend ActiveSupport::Concern

    included do
      class_attribute :cached
      self.cached = false
      delegate :cached?, :to => "self.class"
    end

    module ClassMethods #:nodoc

      # Sets caching on for this class. This class level configuration will
      # default all queries to cache the results of the first iteration over
      # the cursor into an internal array. This should only be used for queries
      # that return a small number of results or have small documents, as after
      # the first iteration the entire results will be stored in memory.
      #
      # @example Cache all reads for the class.
      #   class Person
      #     include Mongoid::Document
      #     cache
      #   end
      def cache
        self.cached = true
      end

      # Determines if the class is cached or not.
      #
      # @example Are class reads cached?
      #   Document.cached?
      #
      # @return [ true, false ] If the reads are cached.
      def cached?
        !!self.cached
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mongoid-2.1.7 lib/mongoid/extras.rb
mongoid-2.1.6 lib/mongoid/extras.rb
mongoid-2.1.5 lib/mongoid/extras.rb
mongoid-2.1.4 lib/mongoid/extras.rb
mongoid-2.1.3 lib/mongoid/extras.rb
mongoid-2.1.2 lib/mongoid/extras.rb
mongoid-2.1.1 lib/mongoid/extras.rb
mongoid-2.1.0 lib/mongoid/extras.rb
mongoid-braxton-2.0.2 lib/mongoid/extras.rb