Module: Mongoid::Clients::Options

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Clients, Mongoid::Criteria
Defined in:
build/mongoid-master/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

Instance Method Details

#collection(parent = nil) ⇒ Mongo::Collection

Get the collection for the document’s current persistence context.

Examples:

Get the collection for the current persistence context.

document.collection

Parameters:

  • parent (Object) (defaults to: nil)

    The parent object whose collection name is used instead of the current persistence context’s collection name.

Returns:

  • (Mongo::Collection)

    The collection for the current persistence context.



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

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

#collection_nameString

Get the collection name for the document’s current persistence context.

Examples:

Get the collection name for the current persistence context.

document.collection_name

Returns:

  • (String)

    The collection name for the current persistence context.



58
59
60
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 58

def collection_name
  persistence_context.collection_name
end

#mongo_clientMongo::Client

Get the database client for the document’s current persistence context.

Examples:

Get the client for the current persistence context.

document.mongo_client

Returns:

  • (Mongo::Client)

    The client for the current persistence context.



69
70
71
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 69

def mongo_client
  persistence_context.client
end

#persistence_contextMongoid::PersistenceContent

Note:

For embedded documents, the persistence context of the root parent document is returned.

Get the document’s current persistence context.

Examples:

Get the current persistence context.

document.persistence_context

Returns:

  • (Mongoid::PersistenceContent)

    The current persistence context.



83
84
85
86
87
88
89
90
91
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 83

def persistence_context
  if embedded? && !_root?
    _root.persistence_context
  else
    PersistenceContext.get(self) ||
        PersistenceContext.get(self.class) ||
        PersistenceContext.new(self.class)
  end
end

#persistence_context?true | false

Note:

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.

Examples:

Get the current persistence context.

document.persistence_context?

Returns:

  • (true | false)

    Whether a persistence context is set.



103
104
105
106
107
108
109
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 103

def persistence_context?
  if embedded? && !_root?
    _root.persistence_context?
  else
    !!(PersistenceContext.get(self) || PersistenceContext.get(self.class))
  end
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



28
29
30
31
32
33
34
35
# File 'build/mongoid-master/lib/mongoid/clients/options.rb', line 28

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