Module: Mongoid::Threaded

Extended by:
Gem::Deprecate, 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.

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



233
234
235
# File 'lib/mongoid/threaded.rb', line 233

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



259
260
261
# File 'lib/mongoid/threaded.rb', line 259

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:

  • The (Class)

    class to check.

Returns:

  • (Array)

    The current autosaves.

Since:

  • 3.0.0



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

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



127
128
129
# File 'lib/mongoid/threaded.rb', line 127

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



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.

Since:

  • 2.1.9



139
140
141
# File 'lib/mongoid/threaded.rb', line 139

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

#client_overrideString, Symbol Also known as: session_override

Get the global client override.

Examples:

Get the global client override.

Threaded.client_override

Returns:

  • (String, Symbol)

    The override.

Since:

  • 5.0.0



175
176
177
# File 'lib/mongoid/threaded.rb', line 175

def client_override
  Thread.current[CLIENT_OVERRIDE_KEY]
end

#client_override=(name) ⇒ String, Symbol Also known as: session_override=

Set the global client override.

Examples:

Set the global client override.

Threaded.client_override = :testing

Parameters:

  • The (String, Symbol)

    global override name.

Returns:

  • (String, Symbol)

    The override.

Since:

  • 3.0.0



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

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

#current_scopeCriteria

Get the current Mongoid scope.

Examples:

Get the scope.

Threaded.current_scope

Returns:

Since:

  • 5.0.0



205
206
207
# File 'lib/mongoid/threaded.rb', line 205

def current_scope
  Thread.current[CURRENT_SCOPE_KEY]
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



219
220
221
# File 'lib/mongoid/threaded.rb', line 219

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



59
60
61
# File 'lib/mongoid/threaded.rb', line 59

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:

  • The (String, Symbol)

    global override name.

Returns:

  • (String, Symbol)

    The override.

Since:

  • 3.0.0



73
74
75
# File 'lib/mongoid/threaded.rb', line 73

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



87
88
89
# File 'lib/mongoid/threaded.rb', line 87

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



151
152
153
# File 'lib/mongoid/threaded.rb', line 151

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



101
102
103
# File 'lib/mongoid/threaded.rb', line 101

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



163
164
165
# File 'lib/mongoid/threaded.rb', line 163

def exit_validate(document)
  validations_for(document.class).delete_one(document._id)
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



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

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



247
248
249
# File 'lib/mongoid/threaded.rb', line 247

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



271
272
273
# File 'lib/mongoid/threaded.rb', line 271

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:

  • The (Class)

    class to check.

Returns:

  • (Array)

    The current validations.

Since:

  • 2.1.9



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

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