Sha256: c6c7948e8ed941765921ba86a3b1ffc9ebf415a4769bb9be3fa3ec57ba064e8a

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 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}"
      listener.send(method_name, *args) if listener.respond_to?(method_name)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signal-0.2.1 lib/signal.rb