Sha256: 376ef822495a597fa15f08b3c820d23b5ed985df40645a96d0e2760e505cac73

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module CottonTail
  module DSL
    # This is the top level DSL for defining the bindings and message routing of
    # a cotton App
    class Routes
      attr_reader :queues

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

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

      # Define a new queue
      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

      # Creates a scope for nested bindings
      #
      # @example
      #   topic 'some.resource' do
      #     handle 'event.updated', lambda do
      #       puts "I'm bound to some.resource.event.updated"
      #     end
      #   end
      #
      # @param [String] routing_prefix The first part of the routing_key
      def topic(routing_prefix, &block)
        topic = Topic.new(routing_prefix, self)
        topic.instance_eval(&block)
      end

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

      def handlers
        @handlers ||= {}
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cotton-tail-0.6.1 lib/cotton_tail/dsl/routes.rb
cotton-tail-0.6.0 lib/cotton_tail/dsl/routes.rb
cotton-tail-0.5.0 lib/cotton_tail/dsl/routes.rb