module Mongoid::Threaded

This module contains logic for easy access to objects that have a lifecycle on the current thread.

Public Instance Methods

autosaved?(document) click to toggle source

Is the document autosaved on the current thread?

@example Is the document autosaved?

Threaded.autosaved?(doc)

@param [ Document ] document The document to check.

@return [ true, false ] If the document is autosaved.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 314
def autosaved?(document)
  autosaves_for(document.class).include?(document.id)
end
autosaves() click to toggle source

Get all autosaves on the current thread.

@example Get all autosaves.

Threaded.autosaves

@return [ Hash ] The current autosaves.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 340
def autosaves
  Thread.current["[mongoid]:autosaves"] ||= {}
end
autosaves_for(klass) click to toggle source

Get all autosaves on the current thread for the class.

@example Get all autosaves.

Threaded.autosaves_for(Person)

@param [ Class ] The class to check.

@return [ Array ] The current autosaves.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 366
def autosaves_for(klass)
  autosaves[klass] ||= []
end
begin_autosave(document) click to toggle source

Begin autosaving a document on the current thread.

@example Begin autosave.

Threaded.begin_autosave(doc)

@param [ Document ] document The document to autosave.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 113
def begin_autosave(document)
  autosaves_for(document.class).push(document.id)
end
begin_execution(name) click to toggle source

Begin entry into a named thread local stack.

@example Begin entry into the stack.

Threaded.begin_execution(:create)

@param [ String ] name The name of the stack

@return [ true ] True.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 21
def begin_execution(name)
  stack(name).push(true)
end
begin_validate(document) click to toggle source

Begin validating a document on the current thread.

@example Begin validation.

Threaded.begin_validate(doc)

@param [ Document ] document The document to validate.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 125
def begin_validate(document)
  validations_for(document.class).push(document.id)
end
database_override() click to toggle source

Get the global database override.

@example Get the global database override.

Threaded.database_override

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 33
def database_override
  Thread.current["[mongoid]:db-override"]
end
database_override=(name) click to toggle source

Set the global database override.

@example Set the global database override.

Threaded.database_override = :testing

@param [ String, Symbol ] The global override name.

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 47
def database_override=(name)
  Thread.current["[mongoid]:db-override"] = name
end
delete_selection(criteria_instance_id) click to toggle source

Delete the field selection on the current thread.

@example Delete the field selection.

Threaded.delete_selection(Person)

@param [ Integer ] criteria_instance_id The criteria instance id.

@return [ Boolean ] Whether there was a field selection.

@since 3.0.7

# File lib/mongoid/threaded.rb, line 260
def delete_selection(criteria_instance_id)
  selections = Thread.current["[mongoid][selections]"]
  return false unless selections
  !!selections.delete(criteria_instance_id)
end
disable_identity_map(option) click to toggle source

Disable the identity map on either the current thread or all threads.

@example Disable the identity map on all threads.

Threaded.disable_identity_map(:all)

@example Disable the identity map on the current thread.

Threaded.disable_identity_map(:current)

@param [ Symbol ] option The disabling option.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 188
def disable_identity_map(option)
  if option == :all
    Thread.list.each do |thread|
      thread["[mongoid]:identity-map-enabled"] = false
    end
  else
    Thread.current["[mongoid]:identity-map-enabled"] = false
  end
end
enable_identity_map(option) click to toggle source

Enable the identity map on either the current thread or all threads.

@example Enable the identity map on all threads.

Threaded.enable_identity_map(:all)

@example Enable the identity map on the current thread.

Threaded.enable_identity_map(:current)

@param [ Symbol ] option The disabling option.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 209
def enable_identity_map(option)
  if option == :all
    Thread.list.each do |thread|
      thread["[mongoid]:identity-map-enabled"] = true
    end
  else
    Thread.current["[mongoid]:identity-map-enabled"] = true
  end
end
executing?(name) click to toggle source

Are in the middle of executing the named stack

@example Are we in the stack execution?

Threaded.executing?(:create)

@param [ Symbol ] name The name of the stack

@return [ true ] If the stack is being executed.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 73
def executing?(name)
  !stack(name).empty?
end
exit_autosave(document) click to toggle source

Exit autosaving a document on the current thread.

@example Exit autosave.

Threaded.exit_autosave(doc)

@param [ Document ] document The document to autosave.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 137
def exit_autosave(document)
  autosaves_for(document.class).delete_one(document.id)
end
exit_execution(name) click to toggle source

Exit from a named thread local stack.

@example Exit from the stack.

Threaded.exit_execution(:create)

@param [ Symbol ] name The name of the stack

@return [ true ] True.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 87
def exit_execution(name)
  stack(name).pop
end
exit_validate(document) click to toggle source

Exit validating a document on the current thread.

@example Exit validation.

Threaded.exit_validate(doc)

@param [ Document ] document The document to validate.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 149
def exit_validate(document)
  validations_for(document.class).delete_one(document.id)
end
identity_map() click to toggle source

Get the identity map off the current thread.

@example Get the identity map.

Threaded.identity_map

@return [ IdentityMap ] The identity map.

@since 2.1.0

# File lib/mongoid/threaded.rb, line 161
def identity_map
  Thread.current["[mongoid]:identity-map"] ||= IdentityMap.new
end
identity_map_enabled?() click to toggle source

Is the identity map enabled on the current thread?

@example Is the identity map enabled?

Threaded.identity_map_enabled?

@return [ true, false ] If the identity map is enabled.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 173
def identity_map_enabled?
  Thread.current["[mongoid]:identity-map-enabled"] != false
end
scope_stack() click to toggle source

Get the mongoid scope stack for chained criteria.

@example Get the scope stack.

Threaded.scope_stack

@return [ Hash ] The scope stack.

@since 2.1.0

# File lib/mongoid/threaded.rb, line 300
def scope_stack
  Thread.current["[mongoid]:scope-stack"] ||= {}
end
selection(criteria_instance_id) click to toggle source

Get the field selection options from the current thread.

@example Get the field selection options.

Threaded.selection

@param [ Integer ] criteria_instance_id The criteria instance id.

@return [ Hash ] The field selection.

@since 2.4.4

# File lib/mongoid/threaded.rb, line 229
def selection(criteria_instance_id)
  selections = Thread.current["[mongoid][selections]"]
  selections[criteria_instance_id] if selections
end
session_override() click to toggle source

Get the global session override.

@example Get the global session override.

Threaded.session_override

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 274
def session_override
  Thread.current["[mongoid]:session-override"]
end
session_override=(name) click to toggle source

Set the global session override.

@example Set the global session override.

Threaded.session_override = :testing

@param [ String, Symbol ] The global override name.

@return [ String, Symbol ] The override.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 288
def session_override=(name)
  Thread.current["[mongoid]:session-override"] = name
end
sessions() click to toggle source

Get the database sessions from the current thread.

@example Get the database sessions.

Threaded.sessions

@return [ Hash ] The sessions.

@since 3.0.0

# File lib/mongoid/threaded.rb, line 59
def sessions
  Thread.current["[mongoid]:sessions"] ||= {}
end
set_selection(criteria_instance_id, value) click to toggle source

Set the field selection on the current thread.

@example Set the field selection.

Threaded.set_selection(Person, { field: 1 })

@param [ Integer ] criteria_instance_id The criteria instance id. @param [ Hash ] value The current field selection.

@return [ Hash ] The field selection.

@since 2.4.4

# File lib/mongoid/threaded.rb, line 245
def set_selection(criteria_instance_id, value)
  Thread.current["[mongoid][selections]"] ||= {}
  Thread.current["[mongoid][selections]"][criteria_instance_id] = value
end
stack(name) click to toggle source

Get the named stack.

@example Get a stack by name

Threaded.stack(:create)

@param [ Symbol ] name The name of the stack

@return [ Array ] The stack.

@since 2.4.0

# File lib/mongoid/threaded.rb, line 101
def stack(name)
  Thread.current["[mongoid]:#{name}-stack"] ||= []
end
validated?(document) click to toggle source

Is the document validated on the current thread?

@example Is the document validated?

Threaded.validated?(doc)

@param [ Document ] document The document to check.

@return [ true, false ] If the document is validated.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 328
def validated?(document)
  validations_for(document.class).include?(document.id)
end
validations() click to toggle source

Get all validations on the current thread.

@example Get all validations.

Threaded.validations

@return [ Hash ] The current validations.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 352
def validations
  Thread.current["[mongoid]:validations"] ||= {}
end
validations_for(klass) click to toggle source

Get all validations on the current thread for the class.

@example Get all validations.

Threaded.validations_for(Person)

@param [ Class ] The class to check.

@return [ Array ] The current validations.

@since 2.1.9

# File lib/mongoid/threaded.rb, line 379
def validations_for(klass)
  validations[klass] ||= []
end