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"
- CLIENTS_KEY =
Constant for the key to store clients.
"[mongoid]:clients"
- CLIENT_OVERRIDE_KEY =
The key to override the client.
"[mongoid]:client-override"
- CURRENT_SCOPE_KEY =
The key for the current thread’s scope stack.
"[mongoid]:current-scope"
- AUTOSAVES_KEY =
"[mongoid]:autosaves"
- VALIDATIONS_KEY =
"[mongoid]:validations"
- STACK_KEYS =
Hash.new do |hash, key| hash[key] = "[mongoid]:#{key}-stack" end
- BIND =
'bind'.freeze
- ASSIGN =
'assign'.freeze
- BUILD =
'build'.freeze
- LOAD =
'load'.freeze
- CREATE =
'create'.freeze
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.
-
#begin_without_default_scope(klass) ⇒ Object
private
Begin suppressing default scopes for given model on the current thread.
-
#clear_session ⇒ nil
Clear the cached session for this thread.
-
#client_override ⇒ String, Symbol
Get the global client override.
-
#client_override=(name) ⇒ String, Symbol
Set the global client override.
-
#current_scope(klass = nil) ⇒ Criteria
Get the current Mongoid scope.
-
#current_scope=(scope) ⇒ Criteria
Set the current Mongoid scope.
-
#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.
-
#exit_without_default_scope(klass) ⇒ Object
private
Exit suppressing default scopes for given model on the current thread.
-
#get_session ⇒ Mongo::Session?
Get the cached session for this thread.
-
#set_current_scope(scope, klass) ⇒ Criteria
Set the current Mongoid scope.
-
#set_session(session) ⇒ Object
Cache a session for this 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.
-
#without_default_scope?(klass) ⇒ Boolean
private
Is the given klass’ default scope suppressed on the current thread?.
Instance Method Details
#autosaved?(document) ⇒ true, false
Is the document autosaved on the current thread?
259 260 261 |
# File 'lib/mongoid/threaded.rb', line 259 def autosaved?(document) autosaves_for(document.class).include?(document._id) end |
#autosaves ⇒ Hash
Get all autosaves on the current thread.
281 282 283 |
# File 'lib/mongoid/threaded.rb', line 281 def autosaves Thread.current[AUTOSAVES_KEY] ||= {} end |
#autosaves_for(klass) ⇒ Array
Get all autosaves on the current thread for the class.
303 304 305 |
# File 'lib/mongoid/threaded.rb', line 303 def autosaves_for(klass) autosaves[klass] ||= [] end |
#begin_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
107 108 109 |
# File 'lib/mongoid/threaded.rb', line 107 def begin_autosave(document) autosaves_for(document.class).push(document._id) end |
#begin_execution(name) ⇒ true
Begin entry into a named thread local stack.
39 40 41 |
# File 'lib/mongoid/threaded.rb', line 39 def begin_execution(name) stack(name).push(true) end |
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
117 118 119 |
# File 'lib/mongoid/threaded.rb', line 117 def begin_validate(document) validations_for(document.class).push(document._id) end |
#begin_without_default_scope(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Begin suppressing default scopes for given model on the current thread.
149 150 151 |
# File 'lib/mongoid/threaded.rb', line 149 def begin_without_default_scope(klass) stack(:without_default_scope).push(klass) end |
#clear_session ⇒ nil
Clear the cached session for this thread.
344 345 346 347 348 |
# File 'lib/mongoid/threaded.rb', line 344 def clear_session session = get_session session.end_session if session Thread.current[:session] = nil end |
#client_override ⇒ String, Symbol
Get the global client override.
171 172 173 |
# File 'lib/mongoid/threaded.rb', line 171 def client_override Thread.current[CLIENT_OVERRIDE_KEY] end |
#client_override=(name) ⇒ String, Symbol
Set the global client override.
183 184 185 |
# File 'lib/mongoid/threaded.rb', line 183 def client_override=(name) Thread.current[CLIENT_OVERRIDE_KEY] = name end |
#current_scope(klass = nil) ⇒ Criteria
Get the current Mongoid scope.
196 197 198 199 200 201 202 203 204 |
# File 'lib/mongoid/threaded.rb', line 196 def current_scope(klass = nil) if klass && Thread.current[CURRENT_SCOPE_KEY].respond_to?(:keys) Thread.current[CURRENT_SCOPE_KEY][ Thread.current[CURRENT_SCOPE_KEY].keys.find { |k| k <= klass } ] else Thread.current[CURRENT_SCOPE_KEY] end end |
#current_scope=(scope) ⇒ Criteria
Set the current Mongoid scope.
214 215 216 |
# File 'lib/mongoid/threaded.rb', line 214 def current_scope=(scope) Thread.current[CURRENT_SCOPE_KEY] = scope end |
#database_override ⇒ String, Symbol
Get the global database override.
49 50 51 |
# File 'lib/mongoid/threaded.rb', line 49 def database_override Thread.current[DATABASE_OVERRIDE_KEY] end |
#database_override=(name) ⇒ String, Symbol
Set the global database override.
61 62 63 |
# File 'lib/mongoid/threaded.rb', line 61 def database_override=(name) Thread.current[DATABASE_OVERRIDE_KEY] = name 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_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
127 128 129 |
# File 'lib/mongoid/threaded.rb', line 127 def exit_autosave(document) autosaves_for(document.class).delete_one(document._id) end |
#exit_execution(name) ⇒ true
Exit from a named thread local stack.
85 86 87 |
# File 'lib/mongoid/threaded.rb', line 85 def exit_execution(name) stack(name).pop end |
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
137 138 139 |
# File 'lib/mongoid/threaded.rb', line 137 def exit_validate(document) validations_for(document.class).delete_one(document._id) end |
#exit_without_default_scope(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Exit suppressing default scopes for given model on the current thread.
161 162 163 |
# File 'lib/mongoid/threaded.rb', line 161 def exit_without_default_scope(klass) stack(:without_default_scope).delete(klass) end |
#get_session ⇒ Mongo::Session?
Get the cached session for this thread.
334 335 336 |
# File 'lib/mongoid/threaded.rb', line 334 def get_session Thread.current[:session] end |
#set_current_scope(scope, klass) ⇒ Criteria
Set the current Mongoid scope. Safe for multi-model scope chaining.
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/mongoid/threaded.rb', line 227 def set_current_scope(scope, klass) if scope.nil? if Thread.current[CURRENT_SCOPE_KEY] Thread.current[CURRENT_SCOPE_KEY].delete(klass) Thread.current[CURRENT_SCOPE_KEY] = nil if Thread.current[CURRENT_SCOPE_KEY].empty? end else Thread.current[CURRENT_SCOPE_KEY] ||= {} Thread.current[CURRENT_SCOPE_KEY][klass] = scope end end |
#set_session(session) ⇒ Object
Cache a session for this thread.
324 325 326 |
# File 'lib/mongoid/threaded.rb', line 324 def set_session(session) Thread.current[:session] = session end |
#stack(name) ⇒ Array
Get the named stack.
97 98 99 |
# File 'lib/mongoid/threaded.rb', line 97 def stack(name) Thread.current[STACK_KEYS[name]] ||= [] end |
#validated?(document) ⇒ true, false
Is the document validated on the current thread?
271 272 273 |
# File 'lib/mongoid/threaded.rb', line 271 def validated?(document) validations_for(document.class).include?(document._id) end |
#validations ⇒ Hash
Get all validations on the current thread.
291 292 293 |
# File 'lib/mongoid/threaded.rb', line 291 def validations Thread.current[VALIDATIONS_KEY] ||= {} end |
#validations_for(klass) ⇒ Array
Get all validations on the current thread for the class.
314 315 316 |
# File 'lib/mongoid/threaded.rb', line 314 def validations_for(klass) validations[klass] ||= [] end |
#without_default_scope?(klass) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Is the given klass’ default scope suppressed on the current thread?
247 248 249 |
# File 'lib/mongoid/threaded.rb', line 247 def without_default_scope?(klass) stack(:without_default_scope).include?(klass) end |