Module: Artoo::Events
- Included in:
- Robot
- Defined in:
- lib/artoo/events.rb
Instance Method Summary collapse
-
#create_proxy_method(k, 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(k) ⇒ Object
A simple loop to create a ‘fake’ anonymous method.
Instance Method Details
#create_proxy_method(k, 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
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/artoo/events.rb', line 13 def create_proxy_method(k, v) proxy_method_name(k).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
4 5 6 7 8 |
# File 'lib/artoo/events.rb', line 4 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(k) ⇒ Object
A simple loop to create a ‘fake’ anonymous method
27 28 29 30 31 32 |
# File 'lib/artoo/events.rb', line 27 def proxy_method_name(k) begin meth = "#{k}_#{Random.rand(999)}" end while respond_to?(meth) meth end |