module Mongoid::Clients::Options
Mixin module included into Mongoid::Document
which gives the ability to manage the database context for persistence and query operations. For example, this includes saving documents to different collections, and reading documents from secondary instances.
Public Instance Methods
Get the collection for the document’s current persistence context.
@example Get the collection for the current persistence context.
document.collection
@param [ Object
] parent The parent object whose collection name is used
instead of the current persistence context's collection name.
@return [ Mongo::Collection ] The collection for the current persistence
context.
# File lib/mongoid/clients/options.rb, line 47 def collection(parent = nil) persistence_context.collection(parent) end
Get the collection name for the document’s current persistence context.
@example Get the collection name for the current persistence context.
document.collection_name
@return [ String ] The collection name for the current persistence
context.
# File lib/mongoid/clients/options.rb, line 58 def collection_name persistence_context.collection_name end
Get the database client for the document’s current persistence context.
@example Get the client for the current persistence context.
document.mongo_client
@return [ Mongo::Client ] The client for the current persistence
context.
# File lib/mongoid/clients/options.rb, line 69 def mongo_client persistence_context.client end
Get the document’s current persistence context.
@note For embedded documents, the persistence context of the
root parent document is returned.
@example Get the current persistence context.
document.persistence_context
@return [ Mongoid::PersistenceContext
] The current persistence
context.
# File lib/mongoid/clients/options.rb, line 83 def persistence_context if embedded? && !_root? _root.persistence_context else PersistenceContext.get(self) || PersistenceContext.get(self.class) || PersistenceContext.new(self.class, default_storage_options) end end
Returns whether a persistence context is set for the document or the document’s class.
@note For embedded documents, the persistence context of the
root parent document is used.
@example Get the current persistence context.
document.persistence_context?
@return [ true | false ] Whether a persistence context is set.
# File lib/mongoid/clients/options.rb, line 103 def persistence_context? if embedded? && !_root? _root.persistence_context? else remembered_storage_options&.any? || PersistenceContext.get(self).present? || PersistenceContext.get(self.class).present? end end
Change the persistence context for this object during the block.
@example Save the current document to a different collection.
model.with(collection: "bands") do |m| m.save end
@param [ Hash | Mongoid::PersistenceContext
] options_or_context
The storage options or a persistence context.
@option options [ String | Symbol ] :collection The collection name. @option options [ String | Symbol ] :database The database name. @option options [ String | Symbol ] :client The client name.
# File lib/mongoid/clients/options.rb, line 28 def with(options_or_context, &block) original_context = PersistenceContext.get(self) original_cluster = persistence_context.cluster set_persistence_context(options_or_context) yield self ensure clear_persistence_context(original_cluster, original_context) end
Private Instance Methods
# File lib/mongoid/clients/options.rb, line 132 def clear_persistence_context(original_cluster = nil, context = nil) PersistenceContext.clear(self, original_cluster, context) end
# File lib/mongoid/clients/options.rb, line 115 def default_storage_options # Nothing is overridden, we use either the default storage_options # or storage_options defined for the document class. return storage_options if Threaded.client_override.nil? && Threaded.database_override.nil? storage_options.tap do |opts| # Globally overridden client replaces client defined for the document class. opts[:client] = Threaded.client_override unless Threaded.client_override.nil? # Globally overridden database replaces database defined for the document class. opts[:database] = Threaded.database_override unless Threaded.database_override.nil? end end
# File lib/mongoid/clients/options.rb, line 128 def set_persistence_context(options_or_context) PersistenceContext.set(self, options_or_context) end