Class: CottonTail::DSL::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/cotton_tail/dsl/routes.rb

Overview

This is the top level DSL for defining the bindings and message routing of a cotton App

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_strategy:, connection:) ⇒ Routes

Returns a new instance of Routes.



10
11
12
13
14
# File 'lib/cotton_tail/dsl/routes.rb', line 10

def initialize(queue_strategy:, connection:)
  @queue_strategy = queue_strategy
  @connection = connection
  @queues = {}
end

Instance Attribute Details

#queuesObject (readonly)

Returns the value of attribute queues.



8
9
10
# File 'lib/cotton_tail/dsl/routes.rb', line 8

def queues
  @queues
end

Instance Method Details

#draw(&block) ⇒ Object



16
17
18
# File 'lib/cotton_tail/dsl/routes.rb', line 16

def draw(&block)
  instance_eval(&block)
end

#handle(key, handler = nil, &block) ⇒ Object



44
45
46
47
# File 'lib/cotton_tail/dsl/routes.rb', line 44

def handle(key, handler = nil, &block)
  handler ||= block
  handlers[key] = handler
end

#handlersObject



49
50
51
# File 'lib/cotton_tail/dsl/routes.rb', line 49

def handlers
  @handlers ||= {}
end

#queue(name, **opts, &block) ⇒ Object

Define a new queue



21
22
23
24
25
26
27
# File 'lib/cotton_tail/dsl/routes.rb', line 21

def queue(name, **opts, &block)
  @queue_strategy.call(name: name, connection: @connection, **opts).tap do |queue_instance|
    @queues[name] = queue_instance
    queue_dsl = Queue.new(name, queue_instance, self)
    queue_dsl.instance_eval(&block) if block_given?
  end
end

#topic(routing_prefix, &block) ⇒ Object

Creates a scope for nested bindings

Examples:

topic 'some.resource' do
  handle 'event.updated', lambda do
    puts "I'm bound to some.resource.event.updated"
  end
end

Parameters:

  • routing_prefix (String)

    The first part of the routing_key



39
40
41
42
# File 'lib/cotton_tail/dsl/routes.rb', line 39

def topic(routing_prefix, &block)
  topic = Topic.new(routing_prefix, self)
  topic.instance_eval(&block)
end