class ActiveSupport::ExecutionWrapper
Attributes
active[RW]
Public Class Methods
register_hook(hook, outer: false)
click to toggle source
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.)
# 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!()
click to toggle source
Run this execution.
Returns an instance, whose complete!
method
must be invoked after the work has been performed.
Where possible, prefer wrap
.
# File lib/active_support/execution_wrapper.rb, line 65 def self.run! if active? Null else new.tap do |instance| success = nil begin instance.run! success = true ensure instance.complete! unless success end end end end
to_complete(*args, &block)
click to toggle source
# 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)
click to toggle source
# File lib/active_support/execution_wrapper.rb, line 16 def self.to_run(*args, &block) set_callback(:run, *args, &block) end
wrap() { || ... }
click to toggle source
Perform the work in the supplied block as an execution.
# File lib/active_support/execution_wrapper.rb, line 82 def self.wrap return yield if active? instance = run! begin yield ensure instance.complete! end end
Public Instance Methods
complete!()
click to toggle source
Complete this in-flight execution. This method must be
called exactly once on the result of any call to run!
.
Where possible, prefer wrap
.
# File lib/active_support/execution_wrapper.rb, line 117 def complete! run_callbacks(:complete) ensure self.class.active.delete Thread.current end
Private Instance Methods
hook_state()
click to toggle source
# File lib/active_support/execution_wrapper.rb, line 124 def hook_state @_hook_state ||= {} end