Module: Mongoid::Clients::Options::ClassMethods

Defined in:
lib/mongoid/clients/options.rb

Instance Method Summary collapse

Instance Method Details

#client_nameString

Get the database client name for the current persistence context of the document class.

Examples:

Get the client name for the current persistence context.

Model.client_name

Returns:

  • (String)

    The database client name for the current persistence context.



131
132
133
# File 'lib/mongoid/clients/options.rb', line 131

def client_name
  persistence_context.client_name
end

#collectionMongo::Collection

Get the collection for the current persistence context of the document class.

Examples:

Get the collection for the current persistence context.

Model.collection

Returns:

  • (Mongo::Collection)

    The collection for the current persistence context.



167
168
169
# File 'lib/mongoid/clients/options.rb', line 167

def collection
  persistence_context.collection
end

#collection_nameString

Get the collection name for the current persistence context of the document class.

Examples:

Get the collection name for the current persistence context.

Model.collection_name

Returns:

  • (String)

    The collection name for the current persistence context.



143
144
145
# File 'lib/mongoid/clients/options.rb', line 143

def collection_name
  persistence_context.collection_name
end

#database_nameString

Get the database name for the current persistence context of the document class.

Examples:

Get the database name for the current persistence context.

Model.database_name

Returns:

  • (String)

    The database name for the current persistence context.



155
156
157
# File 'lib/mongoid/clients/options.rb', line 155

def database_name
  persistence_context.database_name
end

#mongo_clientMongo::Client

Get the client for the current persistence context of the document class.

Examples:

Get the client for the current persistence context.

Model.mongo_client

Returns:

  • (Mongo::Client)

    The client for the current persistence context.



179
180
181
# File 'lib/mongoid/clients/options.rb', line 179

def mongo_client
  persistence_context.client
end

#persistence_contextMongoid::PersistenceContent

Get the current persistence context of the document class. If a persistence context is not set, a new one will be initialized and returned.

Examples:

Get the current persistence context.

Model.persistence_context

Returns:

  • (Mongoid::PersistenceContent)

    The current persistence context.



213
214
215
# File 'lib/mongoid/clients/options.rb', line 213

def persistence_context
  PersistenceContext.get(self) || PersistenceContext.new(self)
end

#with(options, &block) ⇒ Object

Change the persistence context for this class during the block.

Examples:

Save the current document to a different collection.

Model.with(collection: "bands") do |m|
  m.create
end

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String | Symbol)

    The collection name.

  • :database (String | Symbol)

    The database name.

  • :client (String | Symbol)

    The client name.



195
196
197
198
199
200
201
202
# File 'lib/mongoid/clients/options.rb', line 195

def with(options, &block)
  original_context = PersistenceContext.get(self)
  original_cluster = persistence_context.cluster
  PersistenceContext.set(self, options)
  yield self
ensure
  PersistenceContext.clear(self, original_cluster, original_context)
end