Sha256: 4f1f384fc025ac633a998606903fb8e6979e1f9d8686bac61dbb904cd688d56c

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 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 App
      attr_reader :queues

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

      # Define a new queue
      def queue(name, **opts, &block)
        @queue_strategy.call(name: name, **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
        router.route key, handler
      end

      def router
        @router ||= @routing_strategy.call
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cotton-tail-0.2.1 lib/cotton_tail/dsl/app.rb
cotton-tail-0.2.0 lib/cotton_tail/dsl/app.rb
cotton-tail-0.1.2 lib/cotton_tail/dsl/app.rb
cotton-tail-0.1.1 lib/cotton_tail/dsl/app.rb