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.
-
#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.
-
#set_current_scope(scope, klass) ⇒ Criteria
Set the current Mongoid scope.
-
#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?
260 261 262 |
# File 'lib/mongoid/threaded.rb', line 260 def autosaved?(document) autosaves_for(document.class).include?(document._id) end |
#autosaves ⇒ Hash
Get all autosaves on the current thread.
286 287 288 |
# File 'lib/mongoid/threaded.rb', line 286 def autosaves Thread.current[AUTOSAVES_KEY] ||= {} end |
#autosaves_for(klass) ⇒ Array
Get all autosaves on the current thread for the class.
312 313 314 |
# File 'lib/mongoid/threaded.rb', line 312 def autosaves_for(klass) autosaves[klass] ||= [] end |
#begin_autosave(document) ⇒ Object
Begin autosaving a document on the current thread.
126 127 128 |
# File 'lib/mongoid/threaded.rb', line 126 def begin_autosave(document) autosaves_for(document.class).push(document._id) end |
#begin_execution(name) ⇒ true
Begin entry into a named thread local stack.
46 47 48 |
# File 'lib/mongoid/threaded.rb', line 46 def begin_execution(name) stack(name).push(true) end |
#begin_validate(document) ⇒ Object
Begin validating a document on the current thread.
138 139 140 |
# File 'lib/mongoid/threaded.rb', line 138 def begin_validate(document) validations_for(document.class).push(document._id) end |
#client_override ⇒ String, Symbol
Get the global client override.
174 175 176 |
# File 'lib/mongoid/threaded.rb', line 174 def client_override Thread.current[CLIENT_OVERRIDE_KEY] end |
#client_override=(name) ⇒ String, Symbol
Set the global client override.
188 189 190 |
# File 'lib/mongoid/threaded.rb', line 188 def client_override=(name) Thread.current[CLIENT_OVERRIDE_KEY] = name end |
#current_scope(klass = nil) ⇒ Criteria
Get the current Mongoid scope.
203 204 205 206 207 208 209 210 211 |
# File 'lib/mongoid/threaded.rb', line 203 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.
223 224 225 |
# File 'lib/mongoid/threaded.rb', line 223 def current_scope=(scope) Thread.current[CURRENT_SCOPE_KEY] = scope end |
#database_override ⇒ String, Symbol
Get the global database override.
58 59 60 |
# File 'lib/mongoid/threaded.rb', line 58 def database_override Thread.current[DATABASE_OVERRIDE_KEY] end |
#database_override=(name) ⇒ String, Symbol
Set the global database override.
72 73 74 |
# File 'lib/mongoid/threaded.rb', line 72 def database_override=(name) Thread.current[DATABASE_OVERRIDE_KEY] = name end |
#executing?(name) ⇒ true
Are in the middle of executing the named stack
86 87 88 |
# File 'lib/mongoid/threaded.rb', line 86 def executing?(name) !stack(name).empty? end |
#exit_autosave(document) ⇒ Object
Exit autosaving a document on the current thread.
150 151 152 |
# File 'lib/mongoid/threaded.rb', line 150 def exit_autosave(document) autosaves_for(document.class).delete_one(document._id) end |
#exit_execution(name) ⇒ true
Exit from a named thread local stack.
100 101 102 |
# File 'lib/mongoid/threaded.rb', line 100 def exit_execution(name) stack(name).pop end |
#exit_validate(document) ⇒ Object
Exit validating a document on the current thread.
162 163 164 |
# File 'lib/mongoid/threaded.rb', line 162 def exit_validate(document) validations_for(document.class).delete_one(document._id) end |
#set_current_scope(scope, klass) ⇒ Criteria
Set the current Mongoid scope. Safe for multi-model scope chaining.
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/mongoid/threaded.rb', line 238 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 |
#stack(name) ⇒ Array
Get the named stack.
114 115 116 |
# File 'lib/mongoid/threaded.rb', line 114 def stack(name) Thread.current[STACK_KEYS[name]] ||= [] end |
#validated?(document) ⇒ true, false
Is the document validated on the current thread?
274 275 276 |
# File 'lib/mongoid/threaded.rb', line 274 def validated?(document) validations_for(document.class).include?(document._id) end |
#validations ⇒ Hash
Get all validations on the current thread.
298 299 300 |
# File 'lib/mongoid/threaded.rb', line 298 def validations Thread.current[VALIDATIONS_KEY] ||= {} end |
#validations_for(klass) ⇒ Array
Get all validations on the current thread for the class.
325 326 327 |
# File 'lib/mongoid/threaded.rb', line 325 def validations_for(klass) validations[klass] ||= [] end |