Module: Tardvig::Events::Proxy
- Defined in:
- lib/tardvig/events/proxy.rb
Overview
It allows to listens events of object using another object. It is useful when you do not have a direct access to object which you are want to listen on, for example, when you have access only to GameIO from a display.
To proxy an event from an object, you need to add this object to #tracked_list through #spy_on and then you can bind a listener on it through proxy (see #on for syntax).
If you want proxy to forward event to itself even if you have not bound a listener, you can trigger the ‘:wait_for_event` event on the proxy. You should pass the name of the event you are waiting for as an argument.
Instance Method Summary collapse
- #happen(event, data = nil) ⇒ Object
-
#on(event, &listener) ⇒ Object
It works like the original Events#on, but it also can bind listeners to objects from the #tracked_list.
-
#spy_on(objects) ⇒ Object
Adds objects to tracked list so you can bind proxy listeners to them.
-
#tracked_list ⇒ Hash
Objects (with their names as keys) which you can bind proxy listeners to.
Instance Method Details
#happen(event, data = nil) ⇒ Object
28 29 30 31 |
# File 'lib/tardvig/events/proxy.rb', line 28 def happen(event, data = nil) track_event data if event == :wait_for_event super event, data end |
#on(event, &listener) ⇒ Object
It works like the original Events#on, but it also can bind listeners to objects from the #tracked_list.
To do it, you need to set event name using next syntax: ‘tracked_obj_name:event_name`.
‘tracked_obj_name` is DataIdentifier of an object (from #tracked_list) which you want to bind listener to.
‘event_name` is the event of this object. The `listener` will be binded to this event. Important! When you will trigger this event, its name must be a Symbol.
See spec if you have not understood.
48 49 50 51 |
# File 'lib/tardvig/events/proxy.rb', line 48 def on(event, &listener) track_event event super event, &listener end |
#spy_on(objects) ⇒ Object
Adds objects to tracked list so you can bind proxy listeners to them
18 19 20 |
# File 'lib/tardvig/events/proxy.rb', line 18 def spy_on(objects) tracked_list.merge! objects end |
#tracked_list ⇒ Hash
Returns objects (with their names as keys) which you can bind proxy listeners to.
24 25 26 |
# File 'lib/tardvig/events/proxy.rb', line 24 def tracked_list @tracked_list ||= ({}.extend DataIdentifier) end |