Sha256: bfd81cf16c2bba1301cc2f996b3a5a16840bd85c2197dc99b1c94bf9df94c80d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require "wisper/version"
require "wisper/registration/registration"
require "wisper/registration/object"
require "wisper/registration/object/async_listener"
require "wisper/registration/block"
require 'wisper/global_listeners'

module Wisper
  def listeners
    @listeners ||= Set.new
  end

  def add_listener(listener, options = {})
    listeners << ObjectRegistration.new(listener, options)
    self
  end

  alias :subscribe :add_listener

  def add_block_listener(options = {}, &block)
    listeners << BlockRegistration.new(block, options)
    self
  end

  # sugar
  def respond_to(event, &block)
    add_block_listener({:on => event}, &block)
  end

  alias :on :respond_to

  private

  def all_listeners
    listeners.merge(GlobalListeners.listeners)
  end

  def broadcast(event, *args)
    all_listeners.each do | listener |
      listener.broadcast(clean_event(event), *args)
    end
  end

  alias :publish  :broadcast
  alias :announce :broadcast

  def clean_event(event)
    event.to_s.gsub('-', '_')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-1.0.1 lib/wisper.rb