Module: Mongoid::Clients::Options

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#collection(parent = nil) ⇒ Object



30
31
32
# File 'build/mongoid-8.1/lib/mongoid/clients/options.rb', line 30

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

#collection_nameObject



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

def collection_name
  persistence_context.collection_name
end

#mongo_clientObject



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

def mongo_client
  persistence_context.client
end

#persistence_contextObject



42
43
44
45
46
# File 'build/mongoid-8.1/lib/mongoid/clients/options.rb', line 42

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

#persistence_context?Boolean

Returns:



48
49
50
# File 'build/mongoid-8.1/lib/mongoid/clients/options.rb', line 48

def persistence_context?
  !!(PersistenceContext.get(self) || PersistenceContext.get(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



21
22
23
24
25
26
27
28
# File 'build/mongoid-8.1/lib/mongoid/clients/options.rb', line 21

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