Module: Mongoid::Clients::Options
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveSupport::Concern
|
|
Defined in: | lib/mongoid/clients/options.rb |
Overview
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.
Instance Attribute Summary
-
#persistence_context ⇒ Mongoid::PersistenceContext
readonly
Get the document’s current persistence context.
-
#persistence_context? ⇒ true | false
readonly
Returns whether a persistence context is set for the document or the document’s class.
Instance Method Summary
-
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for the document’s current persistence context.
-
#collection_name ⇒ String
Get the collection name for the document’s current persistence context.
-
#mongo_client ⇒ Mongo::Client
Get the database client for the document’s current persistence context.
-
#with(options_or_context, &block)
Change the persistence context for this object during the block.
- #clear_persistence_context(original_cluster = nil, context = nil) private
- #default_storage_options private
- #set_persistence_context(options_or_context) private
Instance Attribute Details
#persistence_context ⇒ Mongoid::PersistenceContext (readonly)
For embedded documents, the persistence context of the root parent document is returned.
Get the document’s current persistence context.
# File 'lib/mongoid/clients/options.rb', line 83
def persistence_context if && !_root? _root.persistence_context else PersistenceContext.get(self) || PersistenceContext.get(self.class) || PersistenceContext.new(self.class, ) end end
#persistence_context? ⇒ true
| false
(readonly)
For embedded documents, the persistence context of the root parent document is used.
Returns whether a persistence context is set for the document or the document’s class.
# File 'lib/mongoid/clients/options.rb', line 103
def persistence_context? if && !_root? _root.persistence_context? else &.any? || PersistenceContext.get(self).present? || PersistenceContext.get(self.class).present? end end
Instance Method Details
#clear_persistence_context(original_cluster = nil, context = nil) (private)
[ GitHub ]# File 'lib/mongoid/clients/options.rb', line 132
def clear_persistence_context(original_cluster = nil, context = nil) PersistenceContext.clear(self, original_cluster, context) end
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for the document’s current persistence context.
# File 'lib/mongoid/clients/options.rb', line 47
def collection(parent = nil) persistence_context.collection(parent) end
#collection_name ⇒ String
Get the collection name for the document’s current persistence context.
# File 'lib/mongoid/clients/options.rb', line 58
def collection_name persistence_context.collection_name end
#default_storage_options (private)
[ GitHub ]# File 'lib/mongoid/clients/options.rb', line 115
def # Nothing is overridden, we use either the default storage_options # or storage_options defined for the document class. return if Threaded.client_override.nil? && Threaded.database_override.nil? .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
#mongo_client ⇒ Mongo::Client
Get the database client for the document’s current persistence context.
# File 'lib/mongoid/clients/options.rb', line 69
def mongo_client persistence_context.client end
#set_persistence_context(options_or_context) (private)
[ GitHub ]# File 'lib/mongoid/clients/options.rb', line 128
def set_persistence_context( ) PersistenceContext.set(self, ) end
#with(options_or_context, &block)
Change the persistence context for this object during the block.
# File 'lib/mongoid/clients/options.rb', line 28
def with(, &block) original_context = PersistenceContext.get(self) original_cluster = persistence_context.cluster set_persistence_context( ) yield self ensure clear_persistence_context(original_cluster, original_context) end