module Artoo::Events
Class that handles events
Public Instance Methods
create_proxy_method(base_name, v)
click to toggle source
Create an anonymous subscription method so we can wrap the subscription method fire into a valid method regardless of where it is defined @param [String] base_name @param [String] v
# 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={})
click to toggle source
Subscribe to an event from a device @param [Device] device @param [Hash] events
# 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)
click to toggle source
A simple loop to create a ‘fake’ anonymous method @return [Method] created method
# 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