lib/wisper.rb in wisper-1.0.1 vs lib/wisper.rb in wisper-1.1.0

- old
+ new

@@ -1,50 +1,19 @@ -require "wisper/version" -require "wisper/registration/registration" -require "wisper/registration/object" -require "wisper/registration/object/async_listener" -require "wisper/registration/block" +require 'set' +require 'wisper/version' +require 'wisper/publisher' +require 'wisper/registration/registration' +require 'wisper/registration/object' +require 'wisper/registration/block' require 'wisper/global_listeners' +require 'wisper/temporary_listeners' module Wisper - def listeners - @listeners ||= Set.new + def self.included(base) + warn "[DEPRECATION] `include Wisper::Publisher` instead of `include Wisper`" + base.class_eval { include Wisper::Publisher } 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('-', '_') + def self.with_listeners(*args, &block) + TemporaryListeners.with(*args, &block) end end