lib/wisper.rb in wisper-0.0.2 vs lib/wisper.rb in wisper-1.0.0
- old
+ new
@@ -1,62 +1,43 @@
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)
- listeners << BlockRegistration.new(block, :on => event)
+ add_block_listener({:on => event}, &block)
end
- class BlockRegistration
- attr_reader :on, :listener
+ alias :on :respond_to
- def initialize(block, options)
- @listener = block
- @on = Array(options.fetch(:on) { 'all' }).map(&:to_s)
- end
-
- def broadcast(event, *args)
- if on.include?(event) || on.include?('all')
- listener.call(*args)
- end
- end
- end
-
- class ObjectRegistration
- attr_reader :on, :listener
-
- def initialize(listener, options)
- @listener = listener
- @method = options[:method]
- @on = Array(options.fetch(:on) { 'all' }).map(&:to_s)
- end
-
- def broadcast(event, *args)
- if (on.include?(event) || on.include?('all')) && listener.respond_to?(event)
- listener.public_send(event, *args)
- end
- end
- end
-
private
def broadcast(event, *args)
listeners.each do | listener |
- listener.broadcast(clean_event(event), *args)
+ listener.broadcast(clean_event(event), *args)
end
end
+
+ alias :publish :broadcast
+ alias :announce :broadcast
def clean_event(event)
event.to_s.gsub('-', '_')
end
end