Sha256: e5794aa76e7da9f66b39d22034832cf118c9555bd11eba62f5bddf86300af580

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

module MessageDriver
  module Middleware
    class MiddlewareStack
      include Enumerable

      attr_reader :destination

      def initialize(destination)
        @destination = destination
        @middlewares = []
      end

      def middlewares
        @middlewares.dup.freeze
      end

      def append(middleware_class, *args)
        middleware = build_middleware(middleware_class, *args)
        @middlewares << middleware
        middleware
      end

      def prepend(middleware_class, *args)
        middleware = build_middleware(middleware_class, *args)
        @middlewares.unshift middleware
        middleware
      end

      def on_publish(body, headers, properties)
        @middlewares.reduce([body, headers, properties]) do |args, middleware|
          middleware.on_publish(*args)
        end
      end

      def on_consume(body, headers, properties)
        @middlewares.reverse.reduce([body, headers, properties]) do |args, middleware|
          middleware.on_consume(*args)
        end
      end

      def empty?
        @middlewares.empty?
      end

      def each
        @middlewares.each { |x| yield x }
      end

      private

      def build_middleware(middleware_type, *args)
        case middleware_type
        when Hash
          BlockMiddleware.new(destination, middleware_type)
        else
          middleware_type.new(destination, *args)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
message-driver-0.7.0 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.6.1 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.6.0 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.5.3 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.5.2 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.5.1 lib/message_driver/middleware/middleware_stack.rb
message-driver-0.5.0 lib/message_driver/middleware/middleware_stack.rb