Class: ActiveSupport::ExecutionWrapper
- Includes:
- Callbacks
- Defined in:
- lib/active_support/execution_wrapper.rb
Defined Under Namespace
Classes: CompleteHook, RunHook
Constant Summary collapse
- Null =
:nodoc:
Object.new
Constants included from Callbacks
Callbacks::CALLBACK_FILTER_TYPES
Class Attribute Summary collapse
-
.active ⇒ Object
Returns the value of attribute active.
Class Method Summary collapse
-
.active? ⇒ Boolean
:nodoc:.
-
.inherited(other) ⇒ Object
:nodoc:.
-
.register_hook(hook, outer: false) ⇒ Object
Register an object to be invoked during both the
runandcompletesteps. -
.run!(reset: false) ⇒ Object
Run this execution.
- .to_complete(*args, &block) ⇒ Object
- .to_run(*args, &block) ⇒ Object
-
.wrap ⇒ Object
Perform the work in the supplied block as an execution.
Instance Method Summary collapse
-
#complete! ⇒ Object
Complete this in-flight execution.
-
#run! ⇒ Object
:nodoc:.
Methods included from Callbacks
Methods included from Concern
#append_features, #class_methods, extended, #included
Class Attribute Details
.active ⇒ Object
Returns the value of attribute active.
97 98 99 |
# File 'lib/active_support/execution_wrapper.rb', line 97 def active @active end |
Class Method Details
.active? ⇒ Boolean
:nodoc:
107 108 109 |
# File 'lib/active_support/execution_wrapper.rb', line 107 def self.active? # :nodoc: @active.key?(Thread.current) end |
.inherited(other) ⇒ Object
:nodoc:
100 101 102 103 |
# File 'lib/active_support/execution_wrapper.rb', line 100 def self.inherited(other) # :nodoc: super other.active = Concurrent::Hash.new end |
.register_hook(hook, outer: false) ⇒ Object
Register an object to be invoked during both the run and complete steps.
hook.complete will be passed the value returned from hook.run, and will only be invoked if run has previously been called. (Mostly, this means it won't be invoked if an exception occurs in a preceding to_run block; all ordinary to_complete blocks are invoked in that situation.)
49 50 51 52 53 54 55 56 57 |
# File 'lib/active_support/execution_wrapper.rb', line 49 def self.register_hook(hook, outer: false) if outer to_run RunHook.new(hook), prepend: true to_complete :after, CompleteHook.new(hook) else to_run RunHook.new(hook) to_complete CompleteHook.new(hook) end end |
.run!(reset: false) ⇒ Object
Run this execution.
Returns an instance, whose complete! method must be invoked after the work has been performed.
Where possible, prefer wrap.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_support/execution_wrapper.rb', line 65 def self.run!(reset: false) if reset lost_instance = active.delete(Thread.current) lost_instance.complete! unless lost_instance.nil? else return Null if active? end new.tap do |instance| success = nil begin instance.run! success = true ensure instance.complete! unless success end end end |
.to_complete(*args, &block) ⇒ Object
20 21 22 |
# File 'lib/active_support/execution_wrapper.rb', line 20 def self.to_complete(*args, &block) set_callback(:complete, *args, &block) end |
.to_run(*args, &block) ⇒ Object
16 17 18 |
# File 'lib/active_support/execution_wrapper.rb', line 16 def self.to_run(*args, &block) set_callback(:run, *args, &block) end |
.wrap ⇒ Object
Perform the work in the supplied block as an execution.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_support/execution_wrapper.rb', line 85 def self.wrap return yield if active? instance = run! begin yield ensure instance.complete! end end |
Instance Method Details
#complete! ⇒ Object
Complete this in-flight execution. This method must be called exactly once on the result of any call to run!.
Where possible, prefer wrap.
120 121 122 123 124 |
# File 'lib/active_support/execution_wrapper.rb', line 120 def complete! run_callbacks(:complete) ensure self.class.active.delete Thread.current end |
#run! ⇒ Object
:nodoc:
111 112 113 114 |
# File 'lib/active_support/execution_wrapper.rb', line 111 def run! # :nodoc: self.class.active[Thread.current] = self run_callbacks(:run) end |