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

#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



404
405
406
# File 'lib/mongoid/threaded.rb', line 404

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



430
431
432
# File 'lib/mongoid/threaded.rb', line 430

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



456
457
458
# File 'lib/mongoid/threaded.rb', line 456

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



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

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



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

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



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

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

#clear_options!Object

Clear out all options set on a one-time basis.

Examples:

Clear out the options.

Threaded.clear_options!

Since:

  • 2.3.0



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

def clear_options!
  self.timeless = false
end

#clear_persistence_options(klass) ⇒ true

Clear out all the persistence options.

Examples:

Clear out the persistence options.

Threaded.clear_persistence_options(Band)

Parameters:

  • klass (Class)

    The model class.

Returns:

  • (true)

    true.

Since:

  • 2.0.0



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

def clear_persistence_options(klass)
  Thread.current["[mongoid][#{klass}]:persistence-options"] = nil
  true
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



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

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



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

def database_override=(name)
  Thread.current["[mongoid]:db-override"] = name
end

#delete_selection(criteria_instance_id) ⇒ Boolean

Delete the field selection on the current thread.

Examples:

Delete the field selection.

Threaded.delete_selection(Person)

Parameters:

  • criteria_instance_id (Integer)

    The criteria instance id.

Returns:

  • (Boolean)

    Whether there was a field selection.

Since:

  • 3.0.7



314
315
316
317
318
# File 'lib/mongoid/threaded.rb', line 314

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) ⇒ Object

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

Examples:

Disable the identity map on all threads.

Threaded.disable_identity_map(:all)

Disable the identity map on the current thread.

Threaded.disable_identity_map(:current)

Parameters:

  • option (Symbol)

    The disabling option.

Since:

  • 3.0.0



213
214
215
216
217
218
219
220
221
# File 'lib/mongoid/threaded.rb', line 213

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) ⇒ Object

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

Examples:

Enable the identity map on all threads.

Threaded.enable_identity_map(:all)

Enable the identity map on the current thread.

Threaded.enable_identity_map(:current)

Parameters:

  • option (Symbol)

    The disabling option.

Since:

  • 3.0.0



234
235
236
237
238
239
240
241
242
# File 'lib/mongoid/threaded.rb', line 234

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) ⇒ 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



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

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



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

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



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

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



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

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

#identity_mapIdentityMap

Get the identity map off the current thread.

Examples:

Get the identity map.

Threaded.identity_map

Returns:

Since:

  • 2.1.0



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

def identity_map
  Thread.current["[mongoid]:identity-map"] ||= IdentityMap.new
end

#identity_map_enabled?true, false

Is the identity map enabled on the current thread?

Examples:

Is the identity map enabled?

Threaded.identity_map_enabled?

Returns:

  • (true, false)

    If the identity map is enabled.

Since:

  • 3.0.0



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

def identity_map_enabled?
  Thread.current["[mongoid]:identity-map-enabled"] != false
end

#persistence_options(klass) ⇒ Hash

Get the persistence options for the current thread.

Examples:

Get the persistence options.

Threaded.persistence_options(Band)

Parameters:

  • klass (Class)

    The model class.

Returns:

  • (Hash)

    The current persistence options.

Since:

  • 2.1.0



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

def persistence_options(klass)
  Thread.current["[mongoid][#{klass}]:persistence-options"]
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



354
355
356
# File 'lib/mongoid/threaded.rb', line 354

def scope_stack
  Thread.current["[mongoid]:scope-stack"] ||= {}
end

#selection(criteria_instance_id) ⇒ Hash

Get the field selection options from the current thread.

Examples:

Get the field selection options.

Threaded.selection

Parameters:

  • criteria_instance_id (Integer)

    The criteria instance id.

Returns:

  • (Hash)

    The field selection.

Since:

  • 2.4.4



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

def selection(criteria_instance_id)
  selections = Thread.current["[mongoid][selections]"]
  selections[criteria_instance_id] if selections
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



328
329
330
# File 'lib/mongoid/threaded.rb', line 328

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



342
343
344
# File 'lib/mongoid/threaded.rb', line 342

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



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

def sessions
  Thread.current["[mongoid]:sessions"] ||= {}
end

#set_persistence_options(klass, options) ⇒ Hash

Set the persistence options on the current thread.

Examples:

Set the persistence options.

Threaded.set_persistence_options(Band, { safe: { fsync: true }})

Parameters:

  • klass (Class)

    The model class.

  • options (Hash)

    The persistence options.

Returns:

  • (Hash)

    The persistence options.

Since:

  • 2.1.0



269
270
271
# File 'lib/mongoid/threaded.rb', line 269

def set_persistence_options(klass, options)
  Thread.current["[mongoid][#{klass}]:persistence-options"] = options
end

#set_selection(criteria_instance_id, value) ⇒ Hash

Set the field selection on the current thread.

Examples:

Set the field selection.

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

Parameters:

  • criteria_instance_id (Integer)

    The criteria instance id.

  • value (Hash)

    The current field selection.

Returns:

  • (Hash)

    The field selection.

Since:

  • 2.4.4



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

def set_selection(criteria_instance_id, value)
  Thread.current["[mongoid][selections]"] ||= {}
  Thread.current["[mongoid][selections]"][criteria_instance_id] = value
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



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

def stack(name)
  Thread.current["[mongoid]:#{name}-stack"] ||= []
end

#timelesstrue, false

Get the value of the one-off timeless call.

Examples:

Get the timeless value.

Threaded.timeless

Returns:

  • (true, false)

    The timeless setting.

Since:

  • 2.3.0



366
367
368
# File 'lib/mongoid/threaded.rb', line 366

def timeless
  !!Thread.current["[mongoid]:timeless"]
end

#timeless=(value) ⇒ Object

Set the value of the one-off timeless call.

Examples:

Set the timeless value.

Threaded.timeless = true

Parameters:

  • value (true, false)

    The value.

Since:

  • 2.3.0



378
379
380
# File 'lib/mongoid/threaded.rb', line 378

def timeless=(value)
  Thread.current["[mongoid]:timeless"] = value
end

#timestamping?true, false

Is the current thread setting timestamps?

Examples:

Is the current thread timestamping?

Threaded.timestamping?

Returns:

  • (true, false)

    If timestamps can be applied.

Since:

  • 2.3.0



390
391
392
# File 'lib/mongoid/threaded.rb', line 390

def timestamping?
  !timeless
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



418
419
420
# File 'lib/mongoid/threaded.rb', line 418

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



442
443
444
# File 'lib/mongoid/threaded.rb', line 442

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



469
470
471
# File 'lib/mongoid/threaded.rb', line 469

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