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

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

- (true, false) autosaved?(document)

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

- (Hash) autosaves

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

- (Array) autosaves_for(klass)

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

- (Object) begin_autosave(document)

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

- (true) begin_execution(name)

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

- (Object) begin_validate(document)

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

- (String, Symbol) database_override

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

- (String, Symbol) database_override=(name)

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

- (true) executing?(name)

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

- (Object) exit_autosave(document)

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

- (true) exit_execution(name)

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

- (Object) exit_validate(document)

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

- (Hash) scope_stack

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

- (String, Symbol) session_override

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

- (String, Symbol) session_override=(name)

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

- (Hash) sessions

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

- (Array) stack(name)

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

- (true, false) validated?(document)

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

- (Hash) validations

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

- (Array) validations_for(klass)

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