Sha256: d2fea8a7ffddf07b9b4b8fcf064d28653471d144e63fa8412fb80f553f8debd5

Contents?: true

Size: 994 Bytes

Versions: 3

Compression:

Stored size: 994 Bytes

Contents

# frozen_string_literal: true

require "signal/version"
require "signal/listener"
require "signal/extensions/active_record"
require "signal/extensions/call"

module Signal
  def on(event, &block)
    listeners << Listener.new(self, __method__, event, &block)
    self
  end

  def before(event, &block)
    listeners << Listener.new(self, __method__, event, &block)
    self
  end

  def after(event, &block)
    listeners << Listener.new(self, __method__, event, &block)
    self
  end

  def add_listener(listener)
    listeners << listener
    self
  end

  def emit(event, *args)
    emit_signal(:before, event, *args)
    emit_signal(:on, event, *args)
    emit_signal(:after, event, *args)
    nil
  end

  def listeners
    @listeners ||= []
  end

  private def emit_signal(type, event, *args)
    listeners.each do |listener|
      method_name = "#{type}_#{event}"

      if listener.respond_to?(method_name, true)
        listener.send(method_name, *args)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
signal-1.3.1 lib/signal.rb
signal-1.3.0 lib/signal.rb
signal-1.2.0 lib/signal.rb