Module: Mongoid::Clients::Options
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Clients, Mongoid::Criteria
- 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.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#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.
-
#persistence_context ⇒ Mongoid::PersistenceContext
Get the document’s current persistence context.
-
#persistence_context? ⇒ true | false
Returns whether a persistence context is set for the document or the document’s class.
-
#with(options_or_context, &block) ⇒ Object
Change the persistence context for this object during the block.
Instance Method Details
#collection(parent = nil) ⇒ Mongo::Collection
Get the collection for the document’s current persistence context.
47 48 49 |
# 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.
58 59 60 |
# File 'lib/mongoid/clients/options.rb', line 58 def collection_name persistence_context.collection_name end |
#mongo_client ⇒ Mongo::Client
Get the database client for the document’s current persistence context.
69 70 71 |
# File 'lib/mongoid/clients/options.rb', line 69 def mongo_client persistence_context.client end |
#persistence_context ⇒ Mongoid::PersistenceContext
For embedded documents, the persistence context of the root parent document is returned.
Get the document’s current persistence context.
83 84 85 86 87 88 89 90 91 |
# 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
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.
103 104 105 106 107 108 109 110 111 |
# 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 |
#with(options_or_context, &block) ⇒ Object
Change the persistence context for this object during the block.
28 29 30 31 32 33 34 35 |
# 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 |