Class: Adhearsion::Events

Inherits:
Object show all
Includes:
HasGuardedHandlers
Defined in:
lib/adhearsion/events.rb

Defined Under Namespace

Classes: ErrorHandler, Message

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



72
73
74
# File 'lib/adhearsion/events.rb', line 72

def method_missing(method_name, *args, &block)
  register_handler method_name, *args, &block
end

Class Method Details

.instanceObject



22
23
24
# File 'lib/adhearsion/events.rb', line 22

def instance
  @@instance || refresh!
end

.method_missing(method_name, *args, &block) ⇒ Object



14
15
16
# File 'lib/adhearsion/events.rb', line 14

def method_missing(method_name, *args, &block)
  instance.send method_name, *args, &block
end

.refresh!Object



26
27
28
# File 'lib/adhearsion/events.rb', line 26

def refresh!
  @@instance = new
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/adhearsion/events.rb', line 18

def respond_to_missing?(method_name, include_private = false)
  instance.respond_to? method_name, include_private
end

Instance Method Details

#draw(&block) ⇒ Object



68
69
70
# File 'lib/adhearsion/events.rb', line 68

def draw(&block)
  instance_exec(&block)
end

#handle_message(message) ⇒ Object



64
65
66
# File 'lib/adhearsion/events.rb', line 64

def handle_message(message)
  trigger_handler message.type, message.object
end

#queueObject



33
34
35
# File 'lib/adhearsion/events.rb', line 33

def queue
  queue? ? @queue : reinitialize_queue!
end

#queue?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/adhearsion/events.rb', line 45

def queue?
  instance_variable_defined? :@queue
end

#reinitialize_queue!Object



49
50
51
52
53
54
55
# File 'lib/adhearsion/events.rb', line 49

def reinitialize_queue!
  GirlFriday.shutdown! if queue?
  # TODO: Extract number of threads to use from Adhearsion.config
  @queue = GirlFriday::WorkQueue.new 'main_queue', :error_handler => ErrorHandler do |message|
    work message
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/adhearsion/events.rb', line 76

def respond_to_missing?(method_name, include_private = false)
  instance_variable_defined?(:@handlers) && @handlers.has_key?(method_name)
end

#trigger(type, object = nil) ⇒ Object



37
38
39
# File 'lib/adhearsion/events.rb', line 37

def trigger(type, object = nil)
  queue.push_async Message.new(type, object)
end

#trigger_immediately(type, object = nil) ⇒ Object



41
42
43
# File 'lib/adhearsion/events.rb', line 41

def trigger_immediately(type, object = nil)
  queue.push_immediately Message.new(type, object)
end

#work(message) ⇒ Object



57
58
59
60
61
62
# File 'lib/adhearsion/events.rb', line 57

def work(message)
  handle_message message
rescue => e
  raise if message.type == :exception
  trigger :exception, e
end