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

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



201
202
203
# File 'lib/mongoid/threaded.rb', line 201

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



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

def autosaves
  Thread.current["[mongoid]:autosaves"] ||= {}
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



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

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



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

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



21
22
23
# File 'lib/mongoid/threaded.rb', line 21

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



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

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



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

def database_override
  Thread.current["[mongoid]:db-override"]
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



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

def database_override=(name)
  Thread.current["[mongoid]:db-override"] = 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



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

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



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

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



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

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



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

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



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

def scope_stack
  Thread.current["[mongoid]:scope-stack"] ||= {}
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



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

def session_override
  Thread.current["[mongoid]:session-override"]
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



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

def session_override=(name)
  Thread.current["[mongoid]:session-override"] = 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



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

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



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

def stack(name)
  Thread.current["[mongoid]:#{name}-stack"] ||= []
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



215
216
217
# File 'lib/mongoid/threaded.rb', line 215

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



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

def validations
  Thread.current["[mongoid]:validations"] ||= {}
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



266
267
268
# File 'lib/mongoid/threaded.rb', line 266

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