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.



133
134
135
# File 'lib/mongoid/clients/options.rb', line 133

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.



169
170
171
# File 'lib/mongoid/clients/options.rb', line 169

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.



145
146
147
# File 'lib/mongoid/clients/options.rb', line 145

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.



157
158
159
# File 'lib/mongoid/clients/options.rb', line 157

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.



181
182
183
# File 'lib/mongoid/clients/options.rb', line 181

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.



215
216
217
# File 'lib/mongoid/clients/options.rb', line 215

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.



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

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