Class: Rubyfox::Client::EventHandler
- Inherits:
-
Object
- Object
- Rubyfox::Client::EventHandler
- Defined in:
- lib/rubyfox/client/event_handler.rb
Instance Method Summary collapse
- #add(*names, &block) ⇒ Object
- #dispatch(event) ⇒ Object
-
#initialize(smartfox) ⇒ EventHandler
constructor
A new instance of EventHandler.
- #register ⇒ Object
- #remove(*names) ⇒ Object
- #unregister ⇒ Object
Constructor Details
#initialize(smartfox) ⇒ EventHandler
Returns a new instance of EventHandler.
6 7 8 9 |
# File 'lib/rubyfox/client/event_handler.rb', line 6 def initialize(smartfox) @handler = Hash.new { |hash, type| hash[type] = [] } @smartfox = smartfox end |
Instance Method Details
#add(*names, &block) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rubyfox/client/event_handler.rb', line 21 def add(*names, &block) names.each do |name| type = Event[name] @handler[type] << block end end |
#dispatch(event) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/rubyfox/client/event_handler.rb', line 35 def dispatch(event) type = event.type @handler[type].each do |handler| handler.call(event) end end |
#register ⇒ Object
11 12 13 14 15 |
# File 'lib/rubyfox/client/event_handler.rb', line 11 def register Event.types.each do |type| @smartfox.add_event_listener Event[type], self end end |
#remove(*names) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/rubyfox/client/event_handler.rb', line 28 def remove(*names) names.each do |name| type = Event[name] @handler[type].clear end end |
#unregister ⇒ Object
17 18 19 |
# File 'lib/rubyfox/client/event_handler.rb', line 17 def unregister @smartfox.remove_all_event_listeners end |