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) ⇒ 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.
45 46 47 |
# File 'lib/mongoid/clients/options.rb', line 45 def collection(parent = nil) persistence_context.collection(parent) end |
#collection_name ⇒ String
Get the collection name for the document's current persistence context.
56 57 58 |
# File 'lib/mongoid/clients/options.rb', line 56 def collection_name persistence_context.collection_name end |
#mongo_client ⇒ Mongo::Client
Get the database client for the document's current persistence context.
67 68 69 |
# File 'lib/mongoid/clients/options.rb', line 67 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.
81 82 83 84 85 86 87 88 89 |
# File 'lib/mongoid/clients/options.rb', line 81 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.
101 102 103 104 105 106 107 108 109 |
# File 'lib/mongoid/clients/options.rb', line 101 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) ⇒ Object
Change the persistence context for this object during the block.
26 27 28 29 30 31 32 33 |
# File 'lib/mongoid/clients/options.rb', line 26 def with() original_context = PersistenceContext.get(self) original_cluster = persistence_context.cluster set_persistence_context() yield self ensure clear_persistence_context(original_cluster, original_context) end |