Module: Workflow::Callbacks::ClassMethods

Defined in:
lib/workflow/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#after_transitionObject

: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 125

#applicable_callback?(callback, procedure) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
279
280
# File 'lib/workflow/callbacks.rb', line 272

def applicable_callback?(callback, procedure)
  arity = procedure.arity
  return true if arity < 0 || arity > 2
  if [:key, :keyreq, :keyrest].include? procedure.parameters[-1][0]
    return true
  else
    return false
  end
end

#around_transitionObject

: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 185

#before_transitionObject

: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 65

#ensure_after_transitions(name = nil, **opts, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/workflow/callbacks.rb', line 29

def ensure_after_transitions(name=nil, **opts, &block)
  _ensure_procs  = [name, block].compact.map do |exe|
    Callback.new(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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/workflow/callbacks.rb', line 43

def on_error(error_class=Exception, **opts, &block)
  _error_procs  = [opts.delete(:rescue)].compact.map do |exe|
    Callback.new(exe)
  end

  _ensure_procs = [opts.delete(:ensure)].compact.map do |exe|
    Callback.new(exe)
  end

  prepend_around_transition **opts do |obj, callbacks|
    begin
      callbacks.call
    rescue error_class => ex
      self.instance_exec(ex, &block) if block_given?
      # block.call(ex) if block_given?
      _error_procs.each {|l| l.callback.call self, ->{}}
    ensure
      _ensure_procs.each {|l| l.callback.call self, ->{}}
    end
  end
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 167

#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 227

#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 107

#skip_after_transitionObject

: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 177

#skip_around_transitionObject

:call-seq: skip_around_transition(names)

Skip a callback around transition. Options are the same as for the standard #around_transition method.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/workflow/callbacks.rb', line 245

[: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, options|
        set_callback(type, callback, TransitionCallbackWrapper.build_wrapper(callback, name), options)
      end
    end

    define_method "prepend_#{callback}_#{type}" do |*names, &blk|
      _insert_callbacks(names, context_attribute, blk) do |name, options|
        set_callback(type, callback, TransitionCallbackWrapper.build_wrapper(callback, name), options.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, options|
        skip_callback(type, callback, TransitionCallbackWrapper.build_wrapper(callback, name), options)
      end
    end

    # *_action is the same as append_*_action
    alias_method :"append_#{callback}_#{type}", :"#{callback}_#{type}"
  end
end

#skip_before_transitionObject

: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 117