Sha256: a8bc483551e21786e82f7402d2bb7cc036fb7ca5b74b3f01bf8d0e7b4622d178

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

module Messaging
  # Public: Used by Messaging.routes to setup the routes for matching messages to callables
  #
  # See routing.rb for more information.
  class Routes
    include Routing

    # Creates a consumer for the default adapter
    #
    # @param name [Symbol] the name of the consumer.
    def consumer(name, **options, &block)
      consumer_definitions[name] = { options: options, block: block }
    end

    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

    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

    # 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.
    def reload_consumer_routes!
      consumers.each do |c|
        c.clear_routes!
        consumer_definitions[c.name].fetch(:block)&.call(c)
      end
    end

    def consumers
      @consumers ||= []
    end

    # Public: Evaluate route definition.
    def draw(&block)
      routing_definition_blocks << block
    end

    def routing_definition_blocks
      @routing_definition_blocks ||= []
    end

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

    private

    def consumer_definitions
      @consumer_definitions ||= {}
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
messaging-4.0.12 lib/messaging/routes.rb
messaging-4.0.11 lib/messaging/routes.rb
messaging-4.0.10 lib/messaging/routes.rb
messaging-4.0.10.pre lib/messaging/routes.rb
messaging-4.0.9 lib/messaging/routes.rb
messaging-4.0.8 lib/messaging/routes.rb
messaging-4.0.7 lib/messaging/routes.rb
messaging-4.0.6 lib/messaging/routes.rb
messaging-4.0.5 lib/messaging/routes.rb
messaging-4.0.4.pre lib/messaging/routes.rb
messaging-4.0.3.pre lib/messaging/routes.rb
messaging-4.0.2.pre lib/messaging/routes.rb
messaging-4.0.1.pre lib/messaging/routes.rb
messaging-4.0.0.pre lib/messaging/routes.rb
messaging-3.8.2 lib/messaging/routes.rb