Module: Mongoid::Threaded

Extended by:
Threaded
Included in:
Threaded
Defined in:
build/mongoid-7.3/lib/mongoid/threaded.rb,
build/mongoid-7.3/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.

Since:

  • 5.0.0

"[mongoid]:clients"
CLIENT_OVERRIDE_KEY =

The key to override the client.

Since:

  • 5.0.0

"[mongoid]:client-override"
CURRENT_SCOPE_KEY =

The key for the current thread’s scope stack.

Since:

  • 2.0.0

"[mongoid]:current-scope"
AUTOSAVES_KEY =
"[mongoid]:autosaves"
VALIDATIONS_KEY =
"[mongoid]:validations"
STACK_KEYS =
Hash.new do |hash, key|
  hash[key] = "[mongoid]:#{key}-stack"
end
BIND =
'bind'.freeze
ASSIGN =
'assign'.freeze
BUILD =
'build'.freeze
LOAD =
'load'.freeze
CREATE =
'create'.freeze

Instance Method Summary collapse

Instance Method Details

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

Since:

  • 2.1.9



298
299
300
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 298

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.

Since:

  • 3.0.0



324
325
326
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 324

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.

Since:

  • 3.0.0



350
351
352
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 350

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.

Since:

  • 3.0.0



128
129
130
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 128

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.

Since:

  • 2.4.0



48
49
50
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 48

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.

Since:

  • 2.1.9



140
141
142
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 140

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.



176
177
178
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 176

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

#clear_sessionnil

Clear the cached session for this thread.

Examples:

Clear this thread’s session.

Threaded.clear_session

Returns:

  • (nil)

Since:

  • 6.4.0



399
400
401
402
403
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 399

def clear_session
  session = get_session
  session.end_session if session
  Thread.current[:session] = nil
end

#client_overrideString, Symbol

Get the global client override.

Examples:

Get the global client override.

Threaded.client_override

Returns:

  • (String, Symbol)

    The override.

Since:

  • 5.0.0



200
201
202
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 200

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.

Since:

  • 3.0.0



214
215
216
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 214

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:

Since:

  • 5.0.0



229
230
231
232
233
234
235
236
237
# File 'build/mongoid-7.3/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) ⇒ Criteria

Set the current Mongoid scope.

Examples:

Set the scope.

Threaded.current_scope = scope

Parameters:

  • scope (Criteria)

    The current scope.

Returns:

Since:

  • 5.0.0



249
250
251
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 249

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.

Since:

  • 3.0.0



60
61
62
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 60

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.

Since:

  • 3.0.0



74
75
76
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 74

def database_override=(name)
  Thread.current[DATABASE_OVERRIDE_KEY] = name
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.

Since:

  • 2.4.0



88
89
90
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 88

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.

Since:

  • 3.0.0



152
153
154
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 152

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.

Since:

  • 2.4.0



102
103
104
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 102

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.

Since:

  • 2.1.9



164
165
166
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 164

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.



188
189
190
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 188

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

#get_sessionMongo::Session?

Get the cached session for this thread.

Examples:

Get the session for this thread.

Threaded.get_session

Returns:

  • (Mongo::Session, nil)

    The session cached on this thread or nil.

Since:

  • 6.4.0



387
388
389
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 387

def get_session
  Thread.current[:session]
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:

Since:

  • 5.0.1



264
265
266
267
268
269
270
271
272
273
274
# File 'build/mongoid-7.3/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) ⇒ Object

Cache a session for this thread.

Examples:

Save a session for this thread.

Threaded.set_session(session)

Parameters:

  • session (Mongo::Session)

    The session to save.

Since:

  • 6.4.0



375
376
377
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 375

def set_session(session)
  Thread.current[:session] = 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.

Since:

  • 2.4.0



116
117
118
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 116

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.

Since:

  • 2.1.9



312
313
314
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 312

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.

Since:

  • 2.1.9



336
337
338
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 336

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.

Since:

  • 2.1.9



363
364
365
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 363

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:



284
285
286
# File 'build/mongoid-7.3/lib/mongoid/threaded.rb', line 284

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