module Audible

Public Instance Methods

on(*events, &block) click to toggle source
# File lib/audible/audible.rb, line 2
def on(*events, &block)
  events.each{|e| attach(e, &block)}
end
relay(source, event, opts ={}) click to toggle source
# File lib/audible/audible.rb, line 6
def relay(source, event, opts ={})
  name = opts[:as] || event
  source.on(event){|e,args| notify name, args.first}
end

Protected Instance Methods

accepts?(e) click to toggle source
# File lib/audible/audible.rb, line 33
def accepts?(e); accept_all_by_default; end
attach(event,&block) click to toggle source
# File lib/audible/audible.rb, line 13
def attach(event,&block)
  fail no_way_to_notify unless block_given?

  listeners_for(event) << block
end
notify(event, *args) click to toggle source
# File lib/audible/audible.rb, line 19
def notify(event, *args)
  the_first_error = nil
  
  listeners_for(event).each do |listener|
    begin
      listener.call event, args
    rescue Exception => e
      the_first_error ||= e
    end
  end
  
  raise the_first_error if the_first_error
end

Private Instance Methods

accept_all_by_default() click to toggle source
# File lib/audible/audible.rb, line 41
def accept_all_by_default; true; end
listeners() click to toggle source
# File lib/audible/audible.rb, line 49
def listeners; @listeners ||= {}; end
listeners_for(event) click to toggle source
# File lib/audible/audible.rb, line 43
def listeners_for(event)
  fail "Event <#{event}> not supported by #{self.class}." unless accepts?(event) === true
  listeners[event] = [] unless listeners.has_key? event
  listeners[event]
end
no_way_to_notify() click to toggle source
# File lib/audible/audible.rb, line 37
def no_way_to_notify
  "No block supplied. How will I notify you?"
end