Class: Autocad::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/autocad/event_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeEventHandler

Returns a new instance of EventHandler.



5
6
7
8
# File 'lib/autocad/event_handler.rb', line 5

def initialize
  @handlers = {}
  @file = File.open("event_handler.log", "w")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(event, *args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/autocad/event_handler.rb', line 19

def method_missing(event, *args)
  if @handlers[event.to_s]
    @handlers[event.to_s].call(*args)
  else
    @file.puts "Unhandled event: #{event} args: #{args}"
    @file.puts "Event class is: #{event.class}, args are: #{args}"
    # event = event.to_sym if event.is_a? String
    # super
  end
end

Instance Method Details

#add_handler(event, &block) ⇒ Object



11
12
13
# File 'lib/autocad/event_handler.rb', line 11

def add_handler(event, &block)
  @handlers[event] = block if block
end

#get_handler(event) ⇒ Object



15
16
17
# File 'lib/autocad/event_handler.rb', line 15

def get_handler(event)
  @handlers[event]
end