module Mongoid::Threaded

This module contains logic for easy access to objects that have a lifecycle on the current thread.

Constants

ASSIGN
AUTOSAVES_KEY
BIND
BUILD
CLIENTS_KEY

Constant for the key to store clients.

@since 5.0.0

CLIENT_OVERRIDE_KEY

The key to override the client.

@since 5.0.0

CREATE
CURRENT_SCOPE_KEY

The key for the current thread’s scope stack.

@since 2.0.0

DATABASE_OVERRIDE_KEY
LOAD
STACK_KEYS
VALIDATIONS_KEY

Public Instance Methods

autosaved?(document) click to toggle source

Is the document autosaved on the current thread?

@example Is the document autosaved?

Threaded.autosaved?(doc)

@param [ Document ] document The document to check.

@return [ true, false ] If the document is autosaved.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 298
def autosaved?(document)
  autosaves_for(document.class).include?(document._id)
end
autosaves() click to toggle source

Get all autosaves on the current thread.

@example Get all autosaves.

Threaded.autosaves

@return [ Hash ] The current autosaves.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 324
def autosaves
  Thread.current[AUTOSAVES_KEY] ||= {}
end
autosaves_for(klass) click to toggle source

Get all autosaves on the current thread for the class.

@example Get all autosaves.

Threaded.autosaves_for(Person)

@param [ Class ] klass The class to check.

@return [ Array ] The current autosaves.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 350
def autosaves_for(klass)
  autosaves[klass] ||= []
end
begin_autosave(document) click to toggle source

Begin autosaving a document on the current thread.

@example Begin autosave.

Threaded.begin_autosave(doc)

@param [ Document ] document The document to autosave.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 128
def begin_autosave(document)
  autosaves_for(document.class).push(document._id)
end
begin_execution(name) click to toggle source

Begin entry into a named thread local stack.

@example Begin entry into the stack.

Threaded.begin_execution(:create)

@param [ String ] name The name of the stack

@return [ true ] True.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 48
def begin_execution(name)
  stack(name).push(true)
end
begin_validate(document) click to toggle source

Begin validating a document on the current thread.

@example Begin validation.

Threaded.begin_validate(doc)

@param [ Document ] document The document to validate.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 140
def begin_validate(document)
  validations_for(document.class).push(document._id)
end
begin_without_default_scope(klass) click to toggle source

Begin suppressing default scopes for given model on the current thread.

@example Begin without default scope stack.

Threaded.begin_without_default_scope(klass)

@param [ Class ] klass The model to suppress default scoping on.

@api private

# File lib/mongoid/threaded.rb, line 176
def begin_without_default_scope(klass)
  stack(:without_default_scope).push(klass)
end
clear_session() click to toggle source

Clear the cached session for this thread.

@example Clear this thread’s session.

Threaded.clear_session

@return [ nil ]

@since 6.4.0

# File lib/mongoid/threaded.rb, line 399
def clear_session
  session = get_session
  session.end_session if session
  Thread.current[:session] = nil
end
client_override() click to toggle source

Get the global client override.

@example Get the global client override.

Threaded.client_override

@return [ String, Symbol ] The override.

@since 5.0.0

# File lib/mongoid/threaded.rb, line 200
def client_override
  Thread.current[CLIENT_OVERRIDE_KEY]
end
client_override=(name) click to toggle source

Set the global client override.

@example Set the global client override.

Threaded.client_override = :testing

@param [ String, Symbol ] name The global override name.

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 214
def client_override=(name)
  Thread.current[CLIENT_OVERRIDE_KEY] = name
end
current_scope(klass = nil) click to toggle source

Get the current Mongoid scope.

@example Get the scope.

Threaded.current_scope(klass)
Threaded.current_scope

@param [ Klass ] klass The class type of the scope.

@return [ Criteria ] The scope.

@since 5.0.0

# File lib/mongoid/threaded.rb, line 229
def current_scope(klass = nil)
  if klass && Thread.current[CURRENT_SCOPE_KEY].respond_to?(:keys)
    Thread.current[CURRENT_SCOPE_KEY][
        Thread.current[CURRENT_SCOPE_KEY].keys.find { |k| k <= klass }
    ]
  else
    Thread.current[CURRENT_SCOPE_KEY]
  end
end
current_scope=(scope) click to toggle source

Set the current Mongoid scope.

@example Set the scope.

Threaded.current_scope = scope

@param [ Criteria ] scope The current scope.

@return [ Criteria ] The scope.

@since 5.0.0

# File lib/mongoid/threaded.rb, line 249
def current_scope=(scope)
  Thread.current[CURRENT_SCOPE_KEY] = scope
end
database_override() click to toggle source

Get the global database override.

@example Get the global database override.

Threaded.database_override

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 60
def database_override
  Thread.current[DATABASE_OVERRIDE_KEY]
end
database_override=(name) click to toggle source

Set the global database override.

@example Set the global database override.

Threaded.database_override = :testing

@param [ String, Symbol ] name The global override name.

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 74
def database_override=(name)
  Thread.current[DATABASE_OVERRIDE_KEY] = name
end
executing?(name) click to toggle source

Are in the middle of executing the named stack

@example Are we in the stack execution?

Threaded.executing?(:create)

@param [ Symbol ] name The name of the stack

@return [ true ] If the stack is being executed.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 88
def executing?(name)
  !stack(name).empty?
end
exit_autosave(document) click to toggle source

Exit autosaving a document on the current thread.

@example Exit autosave.

Threaded.exit_autosave(doc)

@param [ Document ] document The document to autosave.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 152
def exit_autosave(document)
  autosaves_for(document.class).delete_one(document._id)
end
exit_execution(name) click to toggle source

Exit from a named thread local stack.

@example Exit from the stack.

Threaded.exit_execution(:create)

@param [ Symbol ] name The name of the stack

@return [ true ] True.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 102
def exit_execution(name)
  stack(name).pop
end
exit_validate(document) click to toggle source

Exit validating a document on the current thread.

@example Exit validation.

Threaded.exit_validate(doc)

@param [ Document ] document The document to validate.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 164
def exit_validate(document)
  validations_for(document.class).delete_one(document._id)
end
exit_without_default_scope(klass) click to toggle source

Exit suppressing default scopes for given model on the current thread.

@example Exit without default scope stack.

Threaded.exit_without_default_scope(klass)

@param [ Class ] klass The model to unsuppress default scoping on.

@api private

# File lib/mongoid/threaded.rb, line 188
def exit_without_default_scope(klass)
  stack(:without_default_scope).delete(klass)
end
get_session() click to toggle source

Get the cached session for this thread.

@example Get the session for this thread.

Threaded.get_session

@return [ Mongo::Session, nil ] The session cached on this thread or nil.

@since 6.4.0

# File lib/mongoid/threaded.rb, line 387
def get_session
  Thread.current[:session]
end
set_current_scope(scope, klass) click to toggle source

Set the current Mongoid scope. Safe for multi-model scope chaining.

@example Set the scope.

Threaded.current_scope(scope, klass)

@param [ Criteria ] scope The current scope. @param [ Class ] klass The current model class.

@return [ Criteria ] The scope.

@since 5.0.1

# File lib/mongoid/threaded.rb, line 264
def set_current_scope(scope, klass)
  if scope.nil?
    if Thread.current[CURRENT_SCOPE_KEY]
      Thread.current[CURRENT_SCOPE_KEY].delete(klass)
      Thread.current[CURRENT_SCOPE_KEY] = nil if Thread.current[CURRENT_SCOPE_KEY].empty?
    end
  else
    Thread.current[CURRENT_SCOPE_KEY] ||= {}
    Thread.current[CURRENT_SCOPE_KEY][klass] = scope
  end
end
set_session(session) click to toggle source

Cache a session for this thread.

@example Save a session for this thread.

Threaded.set_session(session)

@param [ Mongo::Session ] session The session to save.

@since 6.4.0

# File lib/mongoid/threaded.rb, line 375
def set_session(session)
  Thread.current[:session] = session
end
stack(name) click to toggle source

Get the named stack.

@example Get a stack by name

Threaded.stack(:create)

@param [ Symbol ] name The name of the stack

@return [ Array ] The stack.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 116
def stack(name)
  Thread.current[STACK_KEYS[name]] ||= []
end
validated?(document) click to toggle source

Is the document validated on the current thread?

@example Is the document validated?

Threaded.validated?(doc)

@param [ Document ] document The document to check.

@return [ true, false ] If the document is validated.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 312
def validated?(document)
  validations_for(document.class).include?(document._id)
end
validations() click to toggle source

Get all validations on the current thread.

@example Get all validations.

Threaded.validations

@return [ Hash ] The current validations.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 336
def validations
  Thread.current[VALIDATIONS_KEY] ||= {}
end
validations_for(klass) click to toggle source

Get all validations on the current thread for the class.

@example Get all validations.

Threaded.validations_for(Person)

@param [ Class ] klass The class to check.

@return [ Array ] The current validations.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 363
def validations_for(klass)
  validations[klass] ||= []
end
without_default_scope?(klass) click to toggle source

Is the given klass’ default scope suppressed on the current thread?

@example Is the given klass’ default scope suppressed?

Threaded.without_default_scope?(klass)

@param [ Class ] klass The model to check for default scope suppression.

@api private

# File lib/mongoid/threaded.rb, line 284
def without_default_scope?(klass)
  stack(:without_default_scope).include?(klass)
end