Class: Consumer::Dispatch

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/consumer/dispatch.rb,
lib/consumer/dispatch/substitute.rb

Defined Under Namespace

Modules: Substitute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(handlers = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/consumer/dispatch.rb', line 11

def self.build(handlers=nil)
  handlers = Array(handlers)

  instance = new

  handlers.each do |handler|
    instance.add_handler(handler)
  end

  instance
end

Instance Method Details

#add_handler(handler) ⇒ Object



35
36
37
# File 'lib/consumer/dispatch.rb', line 35

def add_handler(handler)
  handlers << handler
end

#call(message_data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/consumer/dispatch.rb', line 23

def call(message_data)
  logger.trace { "Dispatching event (#{LogText.message_data(message_data)})" }

  handlers.each do |handle|
    handle.(message_data)
  end

  logger.debug { "Event dispatched (#{LogText.message_data(message_data)}, Handlers Count: #{handlers.count})" }

  nil
end

#handlersObject



5
6
7
# File 'lib/consumer/dispatch.rb', line 5

def handlers
  @handlers ||= []
end

#to_procObject



39
40
41
# File 'lib/consumer/dispatch.rb', line 39

def to_proc
  method(:call)
end