Module: Workflow::Callbacks::ClassMethods
- Defined in:
- lib/workflow/callbacks.rb
Constant Summary collapse
- EMPTY_LAMBDA =
-> {}
Instance Method Summary collapse
-
#after_transition ⇒ Object
:call-seq: after_transition(*instance_method_names, options={}) after_transition(options={}, &block).
- #applicable_callback?(_callback, procedure) ⇒ Boolean
-
#around_transition ⇒ Object
:call-seq: around_transition(*instance_method_names, options={}) around_transition(options={}, &block).
-
#before_transition ⇒ Object
:call-seq: before_transition(*instance_method_names, options={}) before_transition(options={}, &block).
- #ensure_after_transitions(name = nil, **opts, &block) ⇒ Object
- #on_error(error_class = Exception, **opts, &block) ⇒ Object
-
#prepend_after_transition(options = {}, &block) ⇒ Object
Something Interesting.
-
#prepend_around_transition(options = {}, &block) ⇒ Object
Something Interesting.
-
#prepend_before_transition(options = {}, &block) ⇒ Object
Something Interesting.
-
#skip_after_transition ⇒ Object
:call-seq: skip_after_transition(names).
-
#skip_around_transition ⇒ Object
:call-seq: skip_around_transition(names).
-
#skip_before_transition ⇒ Object
:call-seq: skip_before_transition(names).
Instance Method Details
#after_transition ⇒ Object
:call-seq: after_transition(*instance_method_names, options={}) after_transition(options={}, &block)
Append a callback after transition.
Instance methods used for before and after transitions
receive no parameters. Instance methods for around transitions will be given a block,
which must be yielded/called in order for the sequence to continue.
Using a block notation, the first parameter will be an instance of the object
under transition, while the second parameter (around transition only) will be
the block which should be called for the sequence to continue.
== Transition Metadata
Within the callback you can access the transition_context instance variable,
which will give you metadata and arguments passed to the transition.
See Workflow::TransitionContext
== Options
=== If / Unless
The callback will run if or unless the named method returns a truthy value.
# Assuming some_instance_method returns a boolean, after_transition :do_something, if: :some_instance_method after_transition :do_something_else, unless: :some_instance_method
=== Only / Except
The callback will run if or unless the event being processed is in the list given
# Run this callback only on the `accept` and `publish` events.
after_transition :do_something, only: [:accept, :publish]
# Run this callback on events other than the `accept` and `publish` events.
after_transition :do_something_else, except: [:accept, :publish]
|
|
# File 'lib/workflow/callbacks.rb', line 128
|
#applicable_callback?(_callback, procedure) ⇒ Boolean
279 280 281 282 283 284 |
# File 'lib/workflow/callbacks.rb', line 279 def applicable_callback?(_callback, procedure) arity = procedure.arity return true if arity.negative? || arity > 2 [:key, :keyreq, :keyrest].include?(procedure.parameters[-1][0]) end |
#around_transition ⇒ Object
:call-seq: around_transition(*instance_method_names, options={}) around_transition(options={}, &block)
Append a callback around transition.
Instance methods used for before and after transitions
receive no parameters. Instance methods for around transitions will be given a block,
which must be yielded/called in order for the sequence to continue.
Using a block notation, the first parameter will be an instance of the object
under transition, while the second parameter (around transition only) will be
the block which should be called for the sequence to continue.
== Transition Metadata
Within the callback you can access the transition_context instance variable,
which will give you metadata and arguments passed to the transition.
See Workflow::TransitionContext
== Options
=== If / Unless
The callback will run if or unless the named method returns a truthy value.
# Assuming some_instance_method returns a boolean, around_transition :do_something, if: :some_instance_method around_transition :do_something_else, unless: :some_instance_method
=== Only / Except
The callback will run if or unless the event being processed is in the list given
# Run this callback only on the `accept` and `publish` events.
around_transition :do_something, only: [:accept, :publish]
# Run this callback on events other than the `accept` and `publish` events.
around_transition :do_something_else, except: [:accept, :publish]
|
|
# File 'lib/workflow/callbacks.rb', line 188
|
#before_transition ⇒ Object
:call-seq: before_transition(*instance_method_names, options={}) before_transition(options={}, &block)
Append a callback before transition.
Instance methods used for before and after transitions
receive no parameters. Instance methods for around transitions will be given a block,
which must be yielded/called in order for the sequence to continue.
Using a block notation, the first parameter will be an instance of the object
under transition, while the second parameter (around transition only) will be
the block which should be called for the sequence to continue.
== Transition Metadata
Within the callback you can access the transition_context instance variable,
which will give you metadata and arguments passed to the transition.
See Workflow::TransitionContext
== Options
=== If / Unless
The callback will run if or unless the named method returns a truthy value.
# Assuming some_instance_method returns a boolean, before_transition :do_something, if: :some_instance_method before_transition :do_something_else, unless: :some_instance_method
=== Only / Except
The callback will run if or unless the event being processed is in the list given
# Run this callback only on the `accept` and `publish` events.
before_transition :do_something, only: [:accept, :publish]
# Run this callback on events other than the `accept` and `publish` events.
before_transition :do_something_else, except: [:accept, :publish]
|
|
# File 'lib/workflow/callbacks.rb', line 68
|
#ensure_after_transitions(name = nil, **opts, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/workflow/callbacks.rb', line 26 def ensure_after_transitions(name = nil, **opts, &block) ensure_procs = [name, block].compact.map do |exe| Callback.build(exe) end prepend_around_transition(**opts) do |obj, callbacks| begin callbacks.call ensure ensure_procs.each { |l| l.callback.call obj, -> {} } end end end |
#on_error(error_class = Exception, **opts, &block) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/workflow/callbacks.rb', line 42 def on_error(error_class = Exception, **opts, &block) error_procs = build_lambdas([opts.delete(:rescue), block]) ensure_procs = build_lambdas(opts.delete(:ensure)) prepend_around_transition(**opts, &on_error_proc(error_class, error_procs, ensure_procs)) end |
#prepend_after_transition(options = {}, &block) ⇒ Object
Something Interesting
Prepend a callback after transition, making it the first after transition called. Options are the same as for the standard #after_transition method.
|
|
# File 'lib/workflow/callbacks.rb', line 170
|
#prepend_around_transition(options = {}, &block) ⇒ Object
Something Interesting
Prepend a callback around transition, making it the first around transition called. Options are the same as for the standard #around_transition method.
|
|
# File 'lib/workflow/callbacks.rb', line 230
|
#prepend_before_transition(options = {}, &block) ⇒ Object
Something Interesting
Prepend a callback before transition, making it the first before transition called. Options are the same as for the standard #before_transition method.
|
|
# File 'lib/workflow/callbacks.rb', line 110
|
#skip_after_transition ⇒ Object
:call-seq: skip_after_transition(names)
Skip a callback after transition. Options are the same as for the standard #after_transition method.
|
|
# File 'lib/workflow/callbacks.rb', line 180
|
#skip_around_transition ⇒ Object
:call-seq: skip_around_transition(names)
Skip a callback around transition. Options are the same as for the standard #around_transition method.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/workflow/callbacks.rb', line 248 [:before, :after, :around].each do |callback| CALLBACK_MAP.each do |type, context_attribute| define_method "#{callback}_#{type}" do |*names, &blk| _insert_callbacks(names, context_attribute, blk) do |name, | set_callback(type, callback, cb(callback, name, self), ) end end define_method "prepend_#{callback}_#{type}" do |*names, &blk| _insert_callbacks(names, context_attribute, blk) do |name, | set_callback(type, callback, cb(callback, name, self), .merge(prepend: true)) end end # Skip a before, after or around callback. See _insert_callbacks # for details on the allowed parameters. define_method "skip_#{callback}_#{type}" do |*names| _insert_callbacks(names, context_attribute) do |name, | skip_callback(type, callback, name, ) end end # *_action is the same as append_*_action alias_method :"append_#{callback}_#{type}", :"#{callback}_#{type}" end end |
#skip_before_transition ⇒ Object
:call-seq: skip_before_transition(names)
Skip a callback before transition. Options are the same as for the standard #before_transition method.
|
|
# File 'lib/workflow/callbacks.rb', line 120
|