Sha256: 671583410085bd1dd6dfe9eda5b8fa94b1e2fac16f063ade80dc991f0592a665

Contents?: true

Size: 864 Bytes

Versions: 1

Compression:

Stored size: 864 Bytes

Contents

require "wisper/version"
require "wisper/registration/registration"
require "wisper/registration/object"
require "wisper/registration/block"

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 broadcast(event, *args)
    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.0 lib/wisper.rb