Sha256: c5ac3baed2a0826efc2adf7d9576dd8cf76a9d3fb58c4da8deac6e508b2d2baa

Contents?: true

Size: 1.94 KB

Versions: 10

Compression:

Stored size: 1.94 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc
  # The collections module is used for providing functionality around setting
  # up and updating collections.
  module Collections
    extend ActiveSupport::Concern
    included do
      cattr_accessor :_collection, :collection_name
      self.collection_name = self.name.collectionize

      delegate :collection, :db, :to => "self.class"
    end

    module ClassMethods #:nodoc:
      # Returns the collection associated with this +Document+. If the
      # document is embedded, there will be no collection associated
      # with it.
      #
      # Returns: <tt>Mongo::Collection</tt>
      def collection
        raise Errors::InvalidCollection.new(self) if embedded?
        self._collection || set_collection
        add_indexes; self._collection
      end

      # Return the database associated with this collection.
      #
      # Example:
      #
      # <tt>Person.db</tt>
      def db
        collection.db
      end

      # Convenience method for getting index information from the collection.
      #
      # Example:
      #
      # <tt>Person.index_information</tt>
      def index_information
        collection.index_information
      end

      # The MongoDB logger is not exposed through the driver to be changed
      # after initialization of the connection, this is a hacky way around that
      # if logging needs to be changed at runtime.
      #
      # Example:
      #
      # <tt>Person.logger = Logger.new($stdout)</tt>
      def logger=(logger)
        db.connection.instance_variable_set(:@logger, logger)
      end

      # Macro for setting the collection name to store in.
      #
      # Example:
      #
      # <tt>Person.store_in :populdation</tt>
      def store_in(name)
        self.collection_name = name.to_s
        set_collection
      end

      protected
      def set_collection
        self._collection = Mongoid::Collection.new(self, self.collection_name)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
mongoid-2.0.0.beta.15 lib/mongoid/collections.rb
mongoid-2.0.0.beta.14 lib/mongoid/collections.rb
mongoid-2.0.0.beta.13 lib/mongoid/collections.rb
mongoid-2.0.0.beta.11 lib/mongoid/collections.rb
mongoid-2.0.0.beta.10 lib/mongoid/collections.rb
mongoid-2.0.0.beta.7 lib/mongoid/collections.rb
mongoid-2.0.0.beta.8 lib/mongoid/collections.rb
mongoid-2.0.0.beta.9 lib/mongoid/collections.rb
mongoid-2.0.0.beta.12 lib/mongoid/collections.rb
mongoid-locomotive-2.0.0.beta9 lib/mongoid/collections.rb