Sha256: 51d682d73214e9c59825d69d5e42ae8eb2fe4f940e3c10028fb3a94c4b9ad77f

Contents?: true

Size: 922 Bytes

Versions: 1

Compression:

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signal-0.2.0 lib/signal.rb