123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Clients::Options

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
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.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#persistence_contextMongoid::PersistenceContext (readonly)

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:

[ GitHub ]

  
# File '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, default_storage_options)
  end
end

#persistence_context?true | false (readonly)

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.

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 103

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

Instance Method Details

#clear_persistence_context(original_cluster = nil, context = nil) (private)

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 132

def clear_persistence_context(original_cluster = nil, context = nil)
  PersistenceContext.clear(self, original_cluster, context)
end

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

[ GitHub ]

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

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 58

def collection_name
  persistence_context.collection_name
end

#default_storage_options (private)

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 115

def default_storage_options
  # Nothing is overridden, we use either the default storage_options
  # or storage_options defined for the document class.
  return storage_options if Threaded.client_override.nil? && Threaded.database_override.nil?

  storage_options.tap do |opts|
    # Globally overridden client replaces client defined for the document class.
    opts[:client] = Threaded.client_override unless Threaded.client_override.nil?
    # Globally overridden database replaces database defined for the document class.
    opts[:database] = Threaded.database_override unless Threaded.database_override.nil?
  end
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.

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 69

def mongo_client
  persistence_context.client
end

#set_persistence_context(options_or_context) (private)

[ GitHub ]

  
# File 'lib/mongoid/clients/options.rb', line 128

def set_persistence_context(options_or_context)
  PersistenceContext.set(self, options_or_context)
end

#with(options_or_context, &block)

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:

[ GitHub ]

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