Sha256: d053af09e98484702f00b1b26cd6219dfc661a322353f44cfed93cb7387b2b57

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Extras #:nodoc:
    def self.included(base)
      base.class_eval do
        extend ClassMethods
        class_inheritable_accessor :cached, :enslaved
        self.cached = false
        self.enslaved = false

        delegate :cached?, :enslaved?, :to => "self.class"
      end
    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:
      #
      #   class Person
      #     include Mongoid::Document
      #     cache
      #   end
      def cache
        self.cached = true
      end

      # Determines if the class is cached or not.
      #
      # Returns:
      #
      # True if cached, false if not.
      def cached?
        !!self.cached
      end

      # Set whether or not this documents read operations should delegate to
      # the slave database by default.
      #
      # Example:
      #
      #   class Person
      #     include Mongoid::Document
      #     enslave
      #   end
      def enslave
        self.enslaved = true
      end

      # Determines if the class is enslaved or not.
      #
      # Returns:
      #
      # True if enslaved, false if not.
      def enslaved?
        !!self.enslaved
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-1.2.14 lib/mongoid/extras.rb
mongoid-1.2.13 lib/mongoid/extras.rb
mongoid-1.2.12 lib/mongoid/extras.rb
mongoid-1.2.11 lib/mongoid/extras.rb
mongoid-1.2.10 lib/mongoid/extras.rb