Module: Exekutor::Internal::Callbacks

Extended by:
ActiveSupport::Concern
Included in:
Executor, Hooks, Provider
Defined in:
lib/exekutor/internal/callbacks.rb

Overview

Mixin to define callbacks on a class

Examples:

Define and call callbacks

class MyClass
  include Exekutor::Internal::Callbacks

  define_callbacks :on_event, :before_another_event, :after_another_event

  def emit_event
    run_callbacks :on, :event, "Callback arg"
  end

  def emit_another_event
    with_callbacks :another_event, self do |self_arg|
      puts "another event"
    end
  end
end
MyClass.new.on_event(12) {|str, int| puts "event happened: #{str}, #{int}" }

Defined Under Namespace

Classes: Error, MissingYield

Instance Method Summary collapse

Instance Method Details

#add_callback(type, *args) {|*args| ... } ⇒ Object

Adds a callback.

Parameters:

  • type (Symbol)

    the type of callback to add

  • args (Any)

    the args to forward to the callback

Yields:

  • the block to call

Yield Parameters:

  • *args (Any)

    the callback args, appended by the specified args

Raises:



38
39
40
41
42
43
44
45
46
# File 'lib/exekutor/internal/callbacks.rb', line 38

def add_callback(type, *args, &callback)
  unless __callback_names.include? type
    raise Error, "Invalid callback type: #{type} (Expected one of: #{__callback_names.map(&:inspect).join(", ")}"
  end
  raise Error, "No callback block supplied" if callback.nil?

  add_callback! type, args, callback
  true
end