module Mongoid::Clients::Options

Public Instance Methods

collection(parent = nil) click to toggle source
# File lib/mongoid/clients/options.rb, line 33
def collection(parent = nil)
  persistence_context.collection(parent)
end
collection_name() click to toggle source
# File lib/mongoid/clients/options.rb, line 37
def collection_name
  persistence_context.collection_name
end
mongo_client() click to toggle source
# File lib/mongoid/clients/options.rb, line 41
def mongo_client
  persistence_context.client
end
persistence_context() click to toggle source
# File 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) { |self| ... } click to toggle source

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.

@since 6.0.0

# File 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

Private Instance Methods

clear_persistence_context(original_cluster = nil, context = nil) click to toggle source
# File lib/mongoid/clients/options.rb, line 57
def clear_persistence_context(original_cluster = nil, context = nil)
  PersistenceContext.clear(self, original_cluster, context)
end
set_persistence_context(options_or_context) click to toggle source
# File lib/mongoid/clients/options.rb, line 53
def set_persistence_context(options_or_context)
  PersistenceContext.set(self, options_or_context)
end