Class: WindowBlessing::EventManager
- Inherits:
-
Object
- Object
- WindowBlessing::EventManager
- Defined in:
- lib/window_blessing/event_manager.rb
Overview
Event handlers are procs which have one input: the event. There can be more than one handler per event-type. Handlers for the same event type are called in the reverse of the order they were added with on. Event handlers return a true value if they handled the event and no more handlers should be called.
Events are hashs. The :type field is a symbol specifying the event type. Other key/values are event-specific
Special handlers:
() => gets all (real) events. Returning true will NOT stop event processing.
All gets access to the events first - and can alter them
All does NOT get :tick events
:unhandled_event => if the event has no handler, this handler is used instead. New event looks like this:
:type => :unhandled_event, :event => unhandled_event.clone
:event_exception => if an exception escaped the event handler, a new event is handed to this handler. New event looks like this:
:type => :event_exception, :event => original_event.clone, :exception => exception_caught, :handler => handler_that_threw_error
Instance Attribute Summary collapse
-
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #handle_event(event) ⇒ Object
- #handle_events(events) ⇒ Object
- #init_standard_handlers ⇒ Object
-
#initialize(parent) ⇒ EventManager
constructor
A new instance of EventManager.
- #inspect ⇒ Object
- #on(*event_type, &block) ⇒ Object
- #on_last(*event_type, &block) ⇒ Object
- #send_to_each_handler(handlers, event) ⇒ Object
Constructor Details
#initialize(parent) ⇒ EventManager
Returns a new instance of EventManager.
20 21 22 23 24 25 |
# File 'lib/window_blessing/event_manager.rb', line 20 def initialize(parent) @parent = parent @event_handlers = {} init_standard_handlers end |
Instance Attribute Details
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
18 19 20 |
# File 'lib/window_blessing/event_manager.rb', line 18 def event_handlers @event_handlers end |
#parent ⇒ Object
Returns the value of attribute parent.
18 19 20 |
# File 'lib/window_blessing/event_manager.rb', line 18 def parent @parent end |
Instance Method Details
#handle_event(event) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/window_blessing/event_manager.rb', line 66 def handle_event(event) type = event[:type] type = [type] unless type.kind_of?(Array) type.length.times.reverse_each do |l| send_to_each_handler(event_handlers[type[0..l]], event) end send_to_each_handler(event_handlers[[]], event) unless type == [:tick] || type == [:unhandled_event] end |
#handle_events(events) ⇒ Object
76 77 78 |
# File 'lib/window_blessing/event_manager.rb', line 76 def handle_events(events) events.each {|event| handle_event(event)} end |
#init_standard_handlers ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/window_blessing/event_manager.rb', line 27 def init_standard_handlers on(:event_exception) do |e| XtermLog.log "#{self.class}(parent=#{parent.inspect}): event_exception: #{e[:exception].inspect} event: #{e[:event].inspect}" XtermLog.log " "+ e[:exception].backtrace.join("\n ") end on(){} end |
#inspect ⇒ Object
35 36 37 |
# File 'lib/window_blessing/event_manager.rb', line 35 def inspect "<#{self.class} :parent => #{parent.inspect} :handled_events => #{event_handlers.keys}>" end |
#on(*event_type, &block) ⇒ Object
39 40 41 42 |
# File 'lib/window_blessing/event_manager.rb', line 39 def on(*event_type, &block) event_handlers[event_type] ||= [] event_handlers[event_type] << block end |
#on_last(*event_type, &block) ⇒ Object
44 45 46 |
# File 'lib/window_blessing/event_manager.rb', line 44 def on_last(*event_type, &block) event_handlers[event_type] = [block] + (event_handlers[event_type] || []) end |
#send_to_each_handler(handlers, event) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/window_blessing/event_manager.rb', line 48 def send_to_each_handler(handlers, event) return if !handlers && event[:type] == :unhandled_event return handle_event :type => :unhandled_event, :event => event.clone unless handlers handlers.reverse_each do |handler| begin handler.call event rescue Exception => e if event[:type] != :event_exception handle_event :type => :event_exception, :event => event.clone, :exception => e, :handler => handler else XtermLog.log "exception in :event_exception handler: #{e.inspect}" end false end end end |