Class: Messaging::Routes

Inherits:
Object
  • Object
show all
Includes:
Routing
Defined in:
lib/messaging/routes.rb

Overview

Public: Used by Messaging.routes to setup the routes for matching messages to callables

See routing.rb for more information.

Instance Method Summary collapse

Methods included from Routing

#clear_routes!, #handle, included, #on

Instance Method Details

#consumer(name, **options, &block) ⇒ Object

Creates a consumer for the default adapter

Parameters:

  • name (Symbol)

    the name of the consumer.



11
12
13
# File 'lib/messaging/routes.rb', line 11

def consumer(name, **options, &block)
  consumer_definitions[name] = { options: options, block: block }
end

#consumersObject



48
49
50
# File 'lib/messaging/routes.rb', line 48

def consumers
  @consumers ||= []
end

#define_consumers!Object



15
16
17
18
19
20
21
22
23
# File 'lib/messaging/routes.rb', line 15

def define_consumers!
  return unless consumers.empty?

  consumer_definitions.each do |name, definition|
    c = Messaging.consumer_adapter.create_consumer(name, **definition.fetch(:options))
    definition.fetch(:block)&.call(c)
    consumers << c
  end
end

#draw(&block) ⇒ Object

Public: Evaluate route definition.



53
54
55
# File 'lib/messaging/routes.rb', line 53

def draw(&block)
  routing_definition_blocks << block
end

#finalize_routesObject



61
62
63
64
65
66
# File 'lib/messaging/routes.rb', line 61

def finalize_routes
  clear_routes!
  routing_definition_blocks.each do |block|
    instance_eval(&block)
  end
end

#inline!(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/messaging/routes.rb', line 25

def inline!(&block)
  current_routes = @routes.dup
  consumer_definitions.each do |_, definition|
    definition.fetch(:block)&.call(self)
  end

  block.call

ensure
  clear_routes!
  @routes = current_routes
end

#reload_consumer_routes!Object

Keeps the consumers, but reload their subscriptions so code reloading works. The consumer has a reference to the class name of each of its handlers, if the handler is reloaded the reference would point to an old instance.



41
42
43
44
45
46
# File 'lib/messaging/routes.rb', line 41

def reload_consumer_routes!
  consumers.each do |c|
    c.clear_routes!
    consumer_definitions[c.name].fetch(:block)&.call(c)
  end
end

#routing_definition_blocksObject



57
58
59
# File 'lib/messaging/routes.rb', line 57

def routing_definition_blocks
  @routing_definition_blocks ||= []
end