Module: Listenable
- Included in:
- IntervalMenu, Menu::DiagramMenu, Menu::IndexOutputMenu, Menu::IndexOverviewMenu, Menu::IndexSelectionMenu, Menu::MainMenu, Menu::SubselectMenu
- Defined in:
- lib/event/listenable.rb
Overview
module to implement simple event handling and notifying
Instance Method Summary collapse
-
#add_listener(identification, listener) ⇒ Object
method to add a listener which wants to be notified when a given event is triggered.
-
#listeners ⇒ Hash
method to get get alle listener or create a new Hash, when not listener is present.
-
#notify_listeners(event_name, *args) ⇒ Object
method to send a notification to all listeners which respond to the given event.
-
#remove_listener(listener) ⇒ Object
method to remove a given listener from the list of listener objects.
Instance Method Details
#add_listener(identification, listener) ⇒ Object
method to add a listener which wants to be notified when a given event is triggered
16 17 18 |
# File 'lib/event/listenable.rb', line 16 def add_listener(identification, listener) listeners[identification] = listener end |
#listeners ⇒ Hash
method to get get alle listener or create a new Hash, when not listener is present
7 8 9 |
# File 'lib/event/listenable.rb', line 7 def listeners @listeners ||= Hash.new() end |
#notify_listeners(event_name, *args) ⇒ Object
method to send a notification to all listeners which respond to the given event
27 28 29 30 31 32 33 |
# File 'lib/event/listenable.rb', line 27 def notify_listeners(event_name, *args) listeners.each_value { |listener| if listener.respond_to?(event_name) listener.__send__(event_name, *args) end } end |
#remove_listener(listener) ⇒ Object
method to remove a given listener from the list of listener objects
21 22 23 |
# File 'lib/event/listenable.rb', line 21 def remove_listener(listener) listeners.delete(listener) end |