Sha256: 1ff2d1f9f0c38f5820dd0924e765afe94755b2316450e9c80030706e163bc862

Contents?: true

Size: 689 Bytes

Versions: 10

Compression:

Stored size: 689 Bytes

Contents

module Fluffle
  class MiddlewareStack
    def initialize
      @stack = []
    end

    def push(middleware)
      @stack << middleware
      self
    end

    alias_method :<<, :push

    # Calls the stack in FIFO order with the callable (passed as an object
    # receiving `#call` or an `&block`) being called last.
    #
    # For example:
    #   stack.push 1
    #   stack.push 2
    #   stack.call 3
    #
    # Will be evaluated 1 -> 2 -> 3 -> 2 -> 1.
    def call(callable = nil, &block)
      callable ||= block

      @stack
        .reverse
        .inject(callable) { |previous, middleware|
          ->{ middleware.call(previous) }
        }
        .call
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fluffle-1.1.0 lib/fluffle/middleware_stack.rb
fluffle-1.0.1 lib/fluffle/middleware_stack.rb
fluffle-1.0.0 lib/fluffle/middleware_stack.rb
fluffle-0.9.1 lib/fluffle/middleware_stack.rb
fluffle-0.9.0 lib/fluffle/middleware_stack.rb
fluffle-0.8.1 lib/fluffle/middleware_stack.rb
fluffle-0.8.0 lib/fluffle/middleware_stack.rb
fluffle-0.7.2 lib/fluffle/middleware_stack.rb
fluffle-0.7.1 lib/fluffle/middleware_stack.rb
fluffle-0.7.0 lib/fluffle/middleware_stack.rb