Sha256: 30668589d96500bd56df9f46946e4b43e56def3080c270238053e41734a871b2

Contents?: true

Size: 851 Bytes

Versions: 2

Compression:

Stored size: 851 Bytes

Contents

require "signal/active_record"
require "signal/listener"
require "signal/version"

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 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)
        listener.public_send(method_name, *args)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
signal-0.1.1 lib/signal.rb
signal-0.1.0 lib/signal.rb