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 collapse
- 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
-
#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_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
-
#begin_execution(name) ⇒ true
Begin entry into a named thread local stack.
-
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
-
#database_override ⇒ String, Symbol
Get the global database override.
-
#database_override=(name) ⇒ String, Symbol
Set the global database override.
-
#executing?(name) ⇒ true
Are in the middle of executing the named stack.
-
#exit_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
-
#exit_execution(name) ⇒ true
Exit from a named thread local stack.
-
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
-
#scope_stack ⇒ Hash
Get the mongoid scope stack for chained criteria.
-
#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.
-
#stack(name) ⇒ Array
Get the named stack.
-
#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?
212 213 214 |
# File 'lib/mongoid/threaded.rb', line 212 def autosaved?(document) autosaves_for(document.class).include?(document._id) end |
#autosaves ⇒ Hash
Get all autosaves on the current thread.
238 239 240 |
# File 'lib/mongoid/threaded.rb', line 238 def autosaves Thread.current[AUTOSAVES_KEY] ||= {} end |
#autosaves_for(klass) ⇒ Array
Get all autosaves on the current thread for the class.
264 265 266 |
# File 'lib/mongoid/threaded.rb', line 264 def autosaves_for(klass) autosaves[klass] ||= [] end |
#begin_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
124 125 126 |
# File 'lib/mongoid/threaded.rb', line 124 def begin_autosave(document) autosaves_for(document.class).push(document._id) end |
#begin_execution(name) ⇒ true
Begin entry into a named thread local stack.
32 33 34 |
# File 'lib/mongoid/threaded.rb', line 32 def begin_execution(name) stack(name).push(true) end |
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
136 137 138 |
# File 'lib/mongoid/threaded.rb', line 136 def begin_validate(document) validations_for(document.class).push(document._id) end |
#database_override ⇒ String, Symbol
Get the global database override.
44 45 46 |
# File 'lib/mongoid/threaded.rb', line 44 def database_override Thread.current[DATABASE_OVERRIDE_KEY] end |
#database_override=(name) ⇒ String, Symbol
Set the global database override.
58 59 60 |
# File 'lib/mongoid/threaded.rb', line 58 def database_override=(name) Thread.current[DATABASE_OVERRIDE_KEY] = name end |
#executing?(name) ⇒ true
Are in the middle of executing the named stack
84 85 86 |
# File 'lib/mongoid/threaded.rb', line 84 def executing?(name) !stack(name).empty? end |
#exit_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
148 149 150 |
# File 'lib/mongoid/threaded.rb', line 148 def exit_autosave(document) autosaves_for(document.class).delete_one(document._id) end |
#exit_execution(name) ⇒ true
Exit from a named thread local stack.
98 99 100 |
# File 'lib/mongoid/threaded.rb', line 98 def exit_execution(name) stack(name).pop end |
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
160 161 162 |
# File 'lib/mongoid/threaded.rb', line 160 def exit_validate(document) validations_for(document.class).delete_one(document._id) end |
#scope_stack ⇒ Hash
Get the mongoid scope stack for chained criteria.
198 199 200 |
# File 'lib/mongoid/threaded.rb', line 198 def scope_stack Thread.current[SCOPE_STACK_KEY] ||= {} end |
#session_override ⇒ String, Symbol
Get the global session override.
172 173 174 |
# File 'lib/mongoid/threaded.rb', line 172 def session_override Thread.current[SESSION_OVERRIDE_KEY] end |
#session_override=(name) ⇒ String, Symbol
Set the global session override.
186 187 188 |
# File 'lib/mongoid/threaded.rb', line 186 def session_override=(name) Thread.current[SESSION_OVERRIDE_KEY] = name end |
#sessions ⇒ Hash
Get the database sessions from the current thread.
70 71 72 |
# File 'lib/mongoid/threaded.rb', line 70 def sessions Thread.current[SESSIONS_KEY] ||= {} end |
#stack(name) ⇒ Array
Get the named stack.
112 113 114 |
# File 'lib/mongoid/threaded.rb', line 112 def stack(name) Thread.current[STACK_KEYS[name]] ||= [] end |
#validated?(document) ⇒ true, false
Is the document validated on the current thread?
226 227 228 |
# File 'lib/mongoid/threaded.rb', line 226 def validated?(document) validations_for(document.class).include?(document._id) end |
#validations ⇒ Hash
Get all validations on the current thread.
250 251 252 |
# File 'lib/mongoid/threaded.rb', line 250 def validations Thread.current[VALIDATIONS_KEY] ||= {} end |
#validations_for(klass) ⇒ Array
Get all validations on the current thread for the class.
277 278 279 |
# File 'lib/mongoid/threaded.rb', line 277 def validations_for(klass) validations[klass] ||= [] end |