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
-
#autosaved?(document) ⇒ true, false
Is the document autosaved on the current thread?.
-
#autosaves ⇒ Hash
Get all autosaves on the current thread.
-
#autosaves_for(klass) ⇒ Array
Get all autosaves on the current thread for the class.
-
#begin(name) ⇒ true
Begin entry into a named thread local stack.
-
#begin_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
-
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
-
#clear_options! ⇒ Object
Clear out all options set on a one-time basis.
-
#clear_persistence_options(klass) ⇒ true
Clear out all the persistence options.
-
#database_override ⇒ String, Symbol
Get the global database override.
-
#database_override=(name) ⇒ String, Symbol
Set the global database override.
-
#disable_identity_map(option) ⇒ Object
Disable the identity map on either the current thread or all threads.
-
#enable_identity_map(option) ⇒ Object
Enable the identity map on either the current thread or all threads.
-
#executing?(name) ⇒ true
Are in the middle of executing the named stack.
-
#exit(name) ⇒ true
Exit from a named thread local stack.
-
#exit_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
-
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
-
#identity_map ⇒ IdentityMap
Get the identity map off the current thread.
-
#identity_map_enabled? ⇒ true, false
Is the identity map enabled on the current thread?.
-
#insert(name) ⇒ Object
Get the insert consumer from the current thread.
-
#persistence_options(klass) ⇒ Hash
Get the persistence options for the current thread.
-
#scope_stack ⇒ Hash
Get the mongoid scope stack for chained criteria.
-
#selection ⇒ Hash
Get the field selection options from the current thread.
-
#selection=(value) ⇒ Hash
Set the field selection on the current thread.
-
#session_override ⇒ String, Symbol
Get the global session override.
-
#session_override=(name) ⇒ String, Symbol
Set the global session override.
-
#sessions ⇒ Hash
Get the database sessions from the current thread.
-
#set_insert(name, consumer) ⇒ Object
Set the insert consumer on the current thread.
-
#set_persistence_options(klass, options) ⇒ Hash
Set the persistence options on the current thread.
-
#stack(name) ⇒ Array
Get the named stack.
-
#timeless ⇒ true, false
Get the value of the one-off timeless call.
-
#timeless=(value) ⇒ Object
Set the value of the one-off timeless call.
-
#timestamping? ⇒ true, false
Is the current thread setting timestamps?.
-
#validated?(document) ⇒ true, false
Is the document validated on the current thread?.
-
#validations ⇒ Hash
Get all validations on the current thread.
-
#validations_for(klass) ⇒ Array
Get all validations on the current thread for the class.
Instance Method Details
#autosaved?(document) ⇒ true, false
Is the document autosaved on the current thread?
409 410 411 |
# File 'lib/mongoid/threaded.rb', line 409 def autosaved?(document) autosaves_for(document.class).include?(document.id) end |
#autosaves ⇒ Hash
Get all autosaves on the current thread.
435 436 437 |
# File 'lib/mongoid/threaded.rb', line 435 def autosaves Thread.current["[mongoid]:autosaves"] ||= {} end |
#autosaves_for(klass) ⇒ Array
Get all autosaves on the current thread for the class.
461 462 463 |
# File 'lib/mongoid/threaded.rb', line 461 def autosaves_for(klass) autosaves[klass] ||= [] end |
#begin(name) ⇒ true
Begin entry into a named thread local stack.
21 22 23 |
# File 'lib/mongoid/threaded.rb', line 21 def begin(name) stack(name).push(true) end |
#begin_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
113 114 115 |
# File 'lib/mongoid/threaded.rb', line 113 def begin_autosave(document) autosaves_for(document.class).push(document.id) end |
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
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.
150 151 152 |
# File 'lib/mongoid/threaded.rb', line 150 def self.timeless = false end |
#clear_persistence_options(klass) ⇒ true
Clear out all the persistence options.
139 140 141 142 |
# File 'lib/mongoid/threaded.rb', line 139 def (klass) Thread.current["[mongoid][#{klass}]:persistence-options"] = nil true end |
#database_override ⇒ String, Symbol
Get the global database override.
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.
47 48 49 |
# File 'lib/mongoid/threaded.rb', line 47 def database_override=(name) Thread.current["[mongoid]:db-override"] = name end |
#disable_identity_map(option) ⇒ Object
Disable the identity map on either the current thread or all threads.
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.
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
73 74 75 |
# File 'lib/mongoid/threaded.rb', line 73 def executing?(name) !stack(name).empty? end |
#exit(name) ⇒ true
Exit from a named thread local stack.
87 88 89 |
# File 'lib/mongoid/threaded.rb', line 87 def exit(name) stack(name).pop end |
#exit_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
162 163 164 |
# File 'lib/mongoid/threaded.rb', line 162 def exit_autosave(document) autosaves_for(document.class).delete_one(document.id) end |
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
174 175 176 |
# File 'lib/mongoid/threaded.rb', line 174 def exit_validate(document) validations_for(document.class).delete_one(document.id) end |
#identity_map ⇒ IdentityMap
Get the identity map off the current thread.
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?
198 199 200 |
# File 'lib/mongoid/threaded.rb', line 198 def identity_map_enabled? Thread.current["[mongoid]:identity-map-enabled"] != false end |
#insert(name) ⇒ Object
Get the insert consumer from the current thread.
252 253 254 |
# File 'lib/mongoid/threaded.rb', line 252 def insert(name) Thread.current["[mongoid][#{name}]:insert-consumer"] end |
#persistence_options(klass) ⇒ Hash
Get the persistence options for the current thread.
280 281 282 |
# File 'lib/mongoid/threaded.rb', line 280 def (klass) Thread.current["[mongoid][#{klass}]:persistence-options"] end |
#scope_stack ⇒ Hash
Get the mongoid scope stack for chained criteria.
359 360 361 |
# File 'lib/mongoid/threaded.rb', line 359 def scope_stack Thread.current["[mongoid]:scope-stack"] ||= {} end |
#selection ⇒ Hash
Get the field selection options from the current thread.
307 308 309 |
# File 'lib/mongoid/threaded.rb', line 307 def selection Thread.current["[mongoid]:selection"] end |
#selection=(value) ⇒ Hash
Set the field selection on the current thread.
321 322 323 |
# File 'lib/mongoid/threaded.rb', line 321 def selection=(value) Thread.current["[mongoid]:selection"] = value end |
#session_override ⇒ String, Symbol
Get the global session override.
333 334 335 |
# File 'lib/mongoid/threaded.rb', line 333 def session_override Thread.current["[mongoid]:session-override"] end |
#session_override=(name) ⇒ String, Symbol
Set the global session override.
347 348 349 |
# File 'lib/mongoid/threaded.rb', line 347 def session_override=(name) Thread.current["[mongoid]:session-override"] = name end |
#sessions ⇒ Hash
Get the database sessions from the current thread.
59 60 61 |
# File 'lib/mongoid/threaded.rb', line 59 def sessions Thread.current["[mongoid]:sessions"] ||= {} end |
#set_insert(name, consumer) ⇒ Object
Set the insert consumer on the current thread.
266 267 268 |
# File 'lib/mongoid/threaded.rb', line 266 def set_insert(name, consumer) Thread.current["[mongoid][#{name}]:insert-consumer"] = consumer end |
#set_persistence_options(klass, options) ⇒ Hash
Set the persistence options on the current thread.
295 296 297 |
# File 'lib/mongoid/threaded.rb', line 295 def (klass, ) Thread.current["[mongoid][#{klass}]:persistence-options"] = end |
#stack(name) ⇒ Array
Get the named stack.
101 102 103 |
# File 'lib/mongoid/threaded.rb', line 101 def stack(name) Thread.current["[mongoid]:#{name}-stack"] ||= [] end |
#timeless ⇒ true, false
Get the value of the one-off timeless call.
371 372 373 |
# File 'lib/mongoid/threaded.rb', line 371 def timeless !!Thread.current["[mongoid]:timeless"] end |
#timeless=(value) ⇒ Object
Set the value of the one-off timeless call.
383 384 385 |
# File 'lib/mongoid/threaded.rb', line 383 def timeless=(value) Thread.current["[mongoid]:timeless"] = value end |
#timestamping? ⇒ true, false
Is the current thread setting timestamps?
395 396 397 |
# File 'lib/mongoid/threaded.rb', line 395 def !timeless end |
#validated?(document) ⇒ true, false
Is the document validated on the current thread?
423 424 425 |
# File 'lib/mongoid/threaded.rb', line 423 def validated?(document) validations_for(document.class).include?(document.id) end |
#validations ⇒ Hash
Get all validations on the current thread.
447 448 449 |
# File 'lib/mongoid/threaded.rb', line 447 def validations Thread.current["[mongoid]:validations"] ||= {} end |
#validations_for(klass) ⇒ Array
Get all validations on the current thread for the class.
474 475 476 |
# File 'lib/mongoid/threaded.rb', line 474 def validations_for(klass) validations[klass] ||= [] end |