Class: Consumer::HandlerRegistry

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

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Method Summary collapse

Methods included from Configure

#configure

Instance Method Details

#countObject



12
13
14
# File 'lib/consumer/handler_registry.rb', line 12

def count
  entries.count
end

#each(&block) ⇒ Object



16
17
18
# File 'lib/consumer/handler_registry.rb', line 16

def each(&block)
  entries.each(&block)
end

#entriesObject



8
9
10
# File 'lib/consumer/handler_registry.rb', line 8

def entries
  @entries ||= Set.new
end

#register(handler) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/consumer/handler_registry.rb', line 20

def register(handler)
  logger.trace { "Registering handler (Handler: #{handler.name})" }

  if registered? handler
    error_message = "Handler is already registered (Handler: #{handler.name})"
    logger.error { error_message }
    raise Error, error_message
  end

  entries << handler

  logger.debug { "Handler registered (Handler: #{handler.name})" }

  handler
end

#registered?(handler) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/consumer/handler_registry.rb', line 36

def registered?(handler)
  entries.any? do |entry|
    return true if handler == entry 

    next if entry.is_a?(Proc)

    handler === entry
  end
end