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"
SESSIONS_KEY =
"[mongoid]:sessions"
SESSION_OVERRIDE_KEY =
"[mongoid]:session-override"
SCOPE_STACK_KEY =
"[mongoid]:scope-stack"
AUTOSAVES_KEY =
"[mongoid]:autosaves"
VALIDATIONS_KEY =
"[mongoid]:validations"
STACK_KEYS =
Hash.new do |hash, key|
  hash[key] = "[mongoid]:#{key}-stack"
end

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



212
213
214
# File 'lib/mongoid/threaded.rb', line 212

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



238
239
240
# File 'lib/mongoid/threaded.rb', line 238

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



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

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



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

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



32
33
34
# File 'lib/mongoid/threaded.rb', line 32

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



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

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



44
45
46
# File 'lib/mongoid/threaded.rb', line 44

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



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

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



84
85
86
# File 'lib/mongoid/threaded.rb', line 84

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



148
149
150
# File 'lib/mongoid/threaded.rb', line 148

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



98
99
100
# File 'lib/mongoid/threaded.rb', line 98

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



160
161
162
# File 'lib/mongoid/threaded.rb', line 160

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

#scope_stackHash

Get the mongoid scope stack for chained criteria.

Examples:

Get the scope stack.

Threaded.scope_stack

Returns:

  • (Hash)

    The scope stack.

Since:

  • 2.1.0



198
199
200
# File 'lib/mongoid/threaded.rb', line 198

def scope_stack
  Thread.current[SCOPE_STACK_KEY] ||= {}
end

#session_overrideString, Symbol

Get the global session override.

Examples:

Get the global session override.

Threaded.session_override

Returns:

  • (String, Symbol)

    The override.

Since:

  • 3.0.0



172
173
174
# File 'lib/mongoid/threaded.rb', line 172

def session_override
  Thread.current[SESSION_OVERRIDE_KEY]
end

#session_override=(name) ⇒ String, Symbol

Set the global session override.

Examples:

Set the global session override.

Threaded.session_override = :testing

Parameters:

  • The (String, Symbol)

    global override name.

Returns:

  • (String, Symbol)

    The override.

Since:

  • 3.0.0



186
187
188
# File 'lib/mongoid/threaded.rb', line 186

def session_override=(name)
  Thread.current[SESSION_OVERRIDE_KEY] = name
end

#sessionsHash

Get the database sessions from the current thread.

Examples:

Get the database sessions.

Threaded.sessions

Returns:

  • (Hash)

    The sessions.

Since:

  • 3.0.0



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

def sessions
  Thread.current[SESSIONS_KEY] ||= {}
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



112
113
114
# File 'lib/mongoid/threaded.rb', line 112

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



226
227
228
# File 'lib/mongoid/threaded.rb', line 226

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



250
251
252
# File 'lib/mongoid/threaded.rb', line 250

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



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

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