Module: Mongoid::Threaded

Extended by:
Threaded
Included in:
Threaded
Defined in:
lib/mongoid/threaded.rb,
lib/mongoid/threaded/lifecycle.rb

Overview

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

Defined Under Namespace

Modules: Lifecycle

Constant Summary collapse

DATABASE_OVERRIDE_KEY =
'[mongoid]:db-override'
CLIENTS_KEY =

Constant for the key to store clients.

'[mongoid]:clients'
CLIENT_OVERRIDE_KEY =

The key to override the client.

'[mongoid]:client-override'
CURRENT_SCOPE_KEY =

The key for the current thread’s scope stack.

'[mongoid]:current-scope'
AUTOSAVES_KEY =
'[mongoid]:autosaves'
VALIDATIONS_KEY =
'[mongoid]:validations'
STACK_KEYS =
Hash.new do |hash, key|
  hash[key] = "[mongoid]:#{key}-stack"
end
SESSIONS_KEY =

The key for the current thread’s sessions.

'[mongoid]:sessions'
MODIFIED_DOCUMENTS_KEY =

The key for storing documents modified inside transactions.

'[mongoid]:modified-documents'
EXECUTE_CALLBACKS =

The key storing the default value for whether or not callbacks are executed on documents.

'[mongoid]:execute-callbacks'
BIND =
'bind'.freeze
ASSIGN =
'assign'.freeze
BUILD =
'build'.freeze
LOAD =
'load'.freeze
CREATE =
'create'.freeze

Instance Method Summary collapse

Instance Method Details

#add_modified_document(session, document) ⇒ Object

Store a reference to the document that was modified inside a transaction associated with the session.

Parameters:

  • session (Mongo::Session)

    Session in scope of which the document was modified.

  • document (Mongoid::Document)

    Mongoid document that was modified.



365
366
367
368
369
# File 'lib/mongoid/threaded.rb', line 365

def add_modified_document(session, document)
  return unless session&.in_transaction?

  modified_documents[session] << document
end

#autosaved?(document) ⇒ true | false

Is the document autosaved on the current thread?

Examples:

Is the document autosaved?

Threaded.autosaved?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true | false)

    If the document is autosaved.



264
265
266
# File 'lib/mongoid/threaded.rb', line 264

def autosaved?(document)
  autosaves_for(document.class).include?(document._id)
end

#autosavesHash

Get all autosaves on the current thread.

Examples:

Get all autosaves.

Threaded.autosaves

Returns:

  • (Hash)

    The current autosaves.



286
287
288
# File 'lib/mongoid/threaded.rb', line 286

def autosaves
  Thread.current[AUTOSAVES_KEY] ||= {}
end

#autosaves_for(klass) ⇒ Array

Get all autosaves on the current thread for the class.

Examples:

Get all autosaves.

Threaded.autosaves_for(Person)

Parameters:

  • klass (Class)

    The class to check.

Returns:

  • (Array)

    The current autosaves.



308
309
310
# File 'lib/mongoid/threaded.rb', line 308

def autosaves_for(klass)
  autosaves[klass] ||= []
end

#begin_autosave(document) ⇒ Object

Begin autosaving a document on the current thread.

Examples:

Begin autosave.

Threaded.begin_autosave(doc)

Parameters:

  • document (Document)

    The document to autosave.



115
116
117
# File 'lib/mongoid/threaded.rb', line 115

def begin_autosave(document)
  autosaves_for(document.class).push(document._id)
end

#begin_execution(name) ⇒ true

Begin entry into a named thread local stack.

Examples:

Begin entry into the stack.

Threaded.begin_execution(:create)

Parameters:

  • name (String)

    The name of the stack

Returns:

  • (true)

    True.



47
48
49
# File 'lib/mongoid/threaded.rb', line 47

def begin_execution(name)
  stack(name).push(true)
end

#begin_validate(document) ⇒ Object

Begin validating a document on the current thread.

Examples:

Begin validation.

Threaded.begin_validate(doc)

Parameters:

  • document (Document)

    The document to validate.



125
126
127
# File 'lib/mongoid/threaded.rb', line 125

def begin_validate(document)
  validations_for(document.class).push(document._id)
end

#begin_without_default_scope(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Begin without default scope stack.

Threaded.begin_without_default_scope(klass)

Parameters:

  • klass (Class)

    The model to suppress default scoping on.



157
158
159
# File 'lib/mongoid/threaded.rb', line 157

def begin_without_default_scope(klass)
  stack(:without_default_scope).push(klass)
end

#clear_modified_documents(session) ⇒ Set<Mongoid::Document>

Clears the set of modified documents for the given session, and return the content of the set before the clearance.

Parameters:

  • session (Mongo::Session)

    Session for which the modified documents set should be cleared.

Returns:

  • (Set<Mongoid::Document>)

    Collection of modified documents before it was cleared.



378
379
380
381
382
# File 'lib/mongoid/threaded.rb', line 378

def clear_modified_documents(session)
  modified_documents[session].dup
ensure
  modified_documents[session].clear
end

#clear_session(client: nil) ⇒ nil

Note:

For backward compatibility it is allowed to call this method without

Clear the cached session for this thread for a client.

specifying ‘client` parameter.

Parameters:

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to clear the session for.

Returns:

  • (nil)


355
356
357
# File 'lib/mongoid/threaded.rb', line 355

def clear_session(client: nil)
  sessions.delete(client)&.end_session
end

#client_overrideString | Symbol

Get the global client override.

Examples:

Get the global client override.

Threaded.client_override

Returns:

  • (String | Symbol)

    The override.



179
180
181
# File 'lib/mongoid/threaded.rb', line 179

def client_override
  Thread.current[CLIENT_OVERRIDE_KEY]
end

#client_override=(name) ⇒ String | Symbol

Set the global client override.

Examples:

Set the global client override.

Threaded.client_override = :testing

Parameters:

  • name (String | Symbol)

    The global override name.

Returns:

  • (String | Symbol)

    The override.



191
192
193
# File 'lib/mongoid/threaded.rb', line 191

def client_override=(name)
  Thread.current[CLIENT_OVERRIDE_KEY] = name
end

#current_scope(klass = nil) ⇒ Criteria

Get the current Mongoid scope.

Examples:

Get the scope.

Threaded.current_scope(klass)
Threaded.current_scope

Parameters:

  • klass (Klass) (defaults to: nil)

    The class type of the scope.

Returns:



204
205
206
207
208
209
210
211
212
# File 'lib/mongoid/threaded.rb', line 204

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) ⇒ Criteria

Set the current Mongoid scope.

Examples:

Set the scope.

Threaded.current_scope = scope

Parameters:

  • scope (Criteria)

    The current scope.

Returns:



222
223
224
# File 'lib/mongoid/threaded.rb', line 222

def current_scope=(scope)
  Thread.current[CURRENT_SCOPE_KEY] = scope
end

#database_overrideString | Symbol

Get the global database override.

Examples:

Get the global database override.

Threaded.database_override

Returns:

  • (String | Symbol)

    The override.



57
58
59
# File 'lib/mongoid/threaded.rb', line 57

def database_override
  Thread.current[DATABASE_OVERRIDE_KEY]
end

#database_override=(name) ⇒ String | Symbol

Set the global database override.

Examples:

Set the global database override.

Threaded.database_override = :testing

Parameters:

  • name (String | Symbol)

    The global override name.

Returns:

  • (String | Symbol)

    The override.



69
70
71
# File 'lib/mongoid/threaded.rb', line 69

def database_override=(name)
  Thread.current[DATABASE_OVERRIDE_KEY] = name
end

#execute_callbacks=(flag) ⇒ Object

Indicates whether document callbacks should be invoked by default for the current thread. Individual documents may further override the callback behavior, but this will be used for the default behavior.

Parameters:

  • flag (true | false)

    Whether or not document callbacks should be executed by default.



406
407
408
# File 'lib/mongoid/threaded.rb', line 406

def execute_callbacks=(flag)
  Thread.current[EXECUTE_CALLBACKS] = flag
end

#execute_callbacks?true | false

Queries whether document callbacks should be executed by default for the current thread.

Unless otherwise indicated (by #execute_callbacks=), this will return true.

Returns:

  • (true | false)

    Whether or not document callbacks should be executed by default.



392
393
394
395
396
397
398
# File 'lib/mongoid/threaded.rb', line 392

def execute_callbacks?
  if Thread.current.key?(EXECUTE_CALLBACKS)
    Thread.current[EXECUTE_CALLBACKS]
  else
    true
  end
end

#executing?(name) ⇒ true

Are in the middle of executing the named stack

Examples:

Are we in the stack execution?

Threaded.executing?(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    If the stack is being executed.



81
82
83
# File 'lib/mongoid/threaded.rb', line 81

def executing?(name)
  !stack(name).empty?
end

#exit_autosave(document) ⇒ Object

Exit autosaving a document on the current thread.

Examples:

Exit autosave.

Threaded.exit_autosave(doc)

Parameters:

  • document (Document)

    The document to autosave.



135
136
137
# File 'lib/mongoid/threaded.rb', line 135

def exit_autosave(document)
  autosaves_for(document.class).delete_one(document._id)
end

#exit_execution(name) ⇒ true

Exit from a named thread local stack.

Examples:

Exit from the stack.

Threaded.exit_execution(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (true)

    True.



93
94
95
# File 'lib/mongoid/threaded.rb', line 93

def exit_execution(name)
  stack(name).pop
end

#exit_validate(document) ⇒ Object

Exit validating a document on the current thread.

Examples:

Exit validation.

Threaded.exit_validate(doc)

Parameters:

  • document (Document)

    The document to validate.



145
146
147
# File 'lib/mongoid/threaded.rb', line 145

def exit_validate(document)
  validations_for(document.class).delete_one(document._id)
end

#exit_without_default_scope(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Exit without default scope stack.

Threaded.exit_without_default_scope(klass)

Parameters:

  • klass (Class)

    The model to unsuppress default scoping on.



169
170
171
# File 'lib/mongoid/threaded.rb', line 169

def exit_without_default_scope(klass)
  stack(:without_default_scope).delete(klass)
end

#get_session(client: nil) ⇒ Mongo::Session | nil

Note:

For backward compatibility it is allowed to call this method without

Get the cached session for this thread for a client.

specifying ‘client` parameter.

Parameters:

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to cache the session for.

Returns:

  • (Mongo::Session | nil)

    The session cached on this thread or nil.



343
344
345
# File 'lib/mongoid/threaded.rb', line 343

def get_session(client: nil)
  sessions[client]
end

#modified_documentsHash<Mongo::Session, Set<Mongoid::Document>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the thread store of modified documents.

Returns:

  • (Hash<Mongo::Session, Set<Mongoid::Document>>)

    The modified documents indexed by session.



425
426
427
428
429
# File 'lib/mongoid/threaded.rb', line 425

def modified_documents
  Thread.current[MODIFIED_DOCUMENTS_KEY] ||= Hash.new do |h, k|
    h[k] = Set.new
  end
end

#sessionsHash<Integer, Set>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the thread store of sessions.

Returns:

  • (Hash<Integer, Set>)

    The sessions indexed by client object ID.



415
416
417
# File 'lib/mongoid/threaded.rb', line 415

def sessions
  Thread.current[SESSIONS_KEY] ||= {}.compare_by_identity
end

#set_current_scope(scope, klass) ⇒ Criteria

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

Examples:

Set the scope.

Threaded.current_scope(scope, klass)

Parameters:

  • scope (Criteria)

    The current scope.

  • klass (Class)

    The current model class.

Returns:



235
236
237
238
239
240
241
242
# File 'lib/mongoid/threaded.rb', line 235

def set_current_scope(scope, klass)
  if scope.nil?
    unset_current_scope(klass)
  else
    Thread.current[CURRENT_SCOPE_KEY] ||= {}
    Thread.current[CURRENT_SCOPE_KEY][klass] = scope
  end
end

#set_session(session, client: nil) ⇒ Object

Note:

For backward compatibility it is allowed to call this method without

Cache a session for this thread for a client.

specifying ‘client` parameter.

Parameters:

  • session (Mongo::Session)

    The session to save.

  • client (Mongo::Client | nil) (defaults to: nil)

    The client to cache the session for.



331
332
333
# File 'lib/mongoid/threaded.rb', line 331

def set_session(session, client: nil)
  sessions[client] = session
end

#stack(name) ⇒ Array

Get the named stack.

Examples:

Get a stack by name

Threaded.stack(:create)

Parameters:

  • name (Symbol)

    The name of the stack

Returns:

  • (Array)

    The stack.



105
106
107
# File 'lib/mongoid/threaded.rb', line 105

def stack(name)
  Thread.current[STACK_KEYS[name]] ||= []
end

#validated?(document) ⇒ true | false

Is the document validated on the current thread?

Examples:

Is the document validated?

Threaded.validated?(doc)

Parameters:

  • document (Document)

    The document to check.

Returns:

  • (true | false)

    If the document is validated.



276
277
278
# File 'lib/mongoid/threaded.rb', line 276

def validated?(document)
  validations_for(document.class).include?(document._id)
end

#validationsHash

Get all validations on the current thread.

Examples:

Get all validations.

Threaded.validations

Returns:

  • (Hash)

    The current validations.



296
297
298
# File 'lib/mongoid/threaded.rb', line 296

def validations
  Thread.current[VALIDATIONS_KEY] ||= {}
end

#validations_for(klass) ⇒ Array

Get all validations on the current thread for the class.

Examples:

Get all validations.

Threaded.validations_for(Person)

Parameters:

  • klass (Class)

    The class to check.

Returns:

  • (Array)

    The current validations.



320
321
322
# File 'lib/mongoid/threaded.rb', line 320

def validations_for(klass)
  validations[klass] ||= []
end

#without_default_scope?(klass) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Is the given klass’ default scope suppressed?

Threaded.without_default_scope?(klass)

Parameters:

  • klass (Class)

    The model to check for default scope suppression.

Returns:



252
253
254
# File 'lib/mongoid/threaded.rb', line 252

def without_default_scope?(klass)
  stack(:without_default_scope).include?(klass)
end