2015-07-30 18:18:12 +02:00
|
|
|
# A simple event registering/triggering module to mix into classes.
|
|
|
|
# Events are stored in the `@events` ivar.
|
2018-03-26 19:05:30 +02:00
|
|
|
module Util
|
|
|
|
module Eventable
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2018-03-26 19:05:30 +02:00
|
|
|
# Risc a handler for the given event name.
|
|
|
|
# The event name is the method name called on the handler object
|
|
|
|
#
|
|
|
|
# obj.on(:foo , some_object_that_implements foo( whateverargs)
|
|
|
|
#
|
|
|
|
# @param [String, Symbol] name event name
|
|
|
|
# @param [Object] object handling the event, ie implement the function name
|
|
|
|
# @return handler
|
|
|
|
def register_event(name, handler)
|
|
|
|
event_table[name] << handler
|
|
|
|
handler
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2018-03-26 19:05:30 +02:00
|
|
|
def unregister_event(name, handler)
|
|
|
|
event_table[name].delete handler
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2018-03-26 19:05:30 +02:00
|
|
|
def event_table
|
|
|
|
return @event_table if @event_table
|
|
|
|
@event_table = Hash.new { |hash, key| hash[key] = [] }
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
|
2018-03-26 19:05:30 +02:00
|
|
|
# Trigger the given event name and passes all args to each handler
|
|
|
|
# for this event.
|
|
|
|
#
|
|
|
|
# obj.trigger(:foo)
|
|
|
|
# obj.trigger(:foo, 1, 2, 3)
|
|
|
|
#
|
|
|
|
# @param [String, Symbol] name event name to trigger
|
|
|
|
def trigger(name, *args)
|
|
|
|
event_table[name].each { |handler| handler.send( name.to_sym , *args) }
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
end
|