Module: Artoo::Events
- Included in:
- Robot
- Defined in:
- lib/artoo/events.rb
Overview
Class that handles events
Instance Method Summary collapse
-
#create_proxy_method(base_name, v) ⇒ Object
Create an anonymous subscription method so we can wrap the subscription method fire into a valid method regardless of where it is defined.
-
#on(device, events = {}) ⇒ Object
Subscribe to an event from a device.
-
#proxy_method_name(base_name) ⇒ Method
A simple loop to create a 'fake' anonymous method.
Instance Method Details
#create_proxy_method(base_name, v) ⇒ Object
Create an anonymous subscription method so we can wrap the subscription method fire into a valid method regardless of where it is defined
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/artoo/events.rb', line 19 def create_proxy_method(base_name, v) proxy_method_name(base_name).tap do |name| self.class.send :define_method, name do |*args| case v when Symbol self.send v.to_sym, *args when Proc v.call(*args) end end end end |
#on(device, events = {}) ⇒ Object
Subscribe to an event from a device
8 9 10 11 12 |
# File 'lib/artoo/events.rb', line 8 def on(device, events={}) events.each do |k, v| subscribe("#{safe_name}_#{device.name}_#{k}", create_proxy_method(k, v)) end end |
#proxy_method_name(base_name) ⇒ Method
A simple loop to create a 'fake' anonymous method
34 35 36 37 38 39 |
# File 'lib/artoo/events.rb', line 34 def proxy_method_name(base_name) begin meth = "#{base_name}_#{Random.rand(999)}" end while respond_to?(meth) meth end |