Module: Mongoid::Clients::Options

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Clients, Mongoid::Criteria
Defined in:
build/mongoid-7.3/lib/mongoid/clients/options.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#collection(parent = nil) ⇒ Object



33
34
35
# File 'build/mongoid-7.3/lib/mongoid/clients/options.rb', line 33

def collection(parent = nil)
  persistence_context.collection(parent)
end

#collection_nameObject



37
38
39
# File 'build/mongoid-7.3/lib/mongoid/clients/options.rb', line 37

def collection_name
  persistence_context.collection_name
end

#mongo_clientObject



41
42
43
# File 'build/mongoid-7.3/lib/mongoid/clients/options.rb', line 41

def mongo_client
  persistence_context.client
end

#persistence_contextObject



45
46
47
48
49
# File 'build/mongoid-7.3/lib/mongoid/clients/options.rb', line 45

def persistence_context
  PersistenceContext.get(self) ||
      PersistenceContext.get(self.class) ||
      PersistenceContext.new(self.class)
end

#with(options_or_context, &block) ⇒ Object

Change the persistence context for this object during the block.

Examples:

Save the current document to a different collection.

model.with(collection: "bands") do |m|
  m.save
end

Parameters:

  • options_or_context (Hash, Mongoid::PersistenceContext)

    The storage options or a persistence context.

  • options (Hash)

    a customizable set of options

Since:

  • 6.0.0



24
25
26
27
28
29
30
31
# File 'build/mongoid-7.3/lib/mongoid/clients/options.rb', line 24

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