Sha256: 97eadc2f09dd577c3a879b2a02ae592062974983a459c5818bcaa5d8ede8c820

Contents?: true

Size: 1.99 KB

Versions: 8

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require "rack/builder"

module Hanami
  class Application
    module Routing
      # Hanami::Applicatione::Router middleware stack
      #
      # @since 2.0.0
      # @api private
      module Middleware
        # Middleware stack
        #
        # @since 2.0.0
        # @api private
        class Stack
          # @since 2.0.0
          # @api private
          ROOT_PREFIX = "/"
          private_constant :ROOT_PREFIX

          # @since 2.0.0
          # @api private
          def initialize
            @prefix = ROOT_PREFIX
            @stack = Hash.new { |hash, key| hash[key] = [] }
          end

          # @since 2.0.0
          # @api private
          def use(middleware, *args, &blk)
            @stack[@prefix].push([middleware, args, blk])
          end

          # @since 2.0.0
          # @api private
          def with(path)
            prefix = @prefix
            @prefix = path
            yield
          ensure
            @prefix = prefix
          end

          # @since 2.0.0
          # @api private
          def to_rack_app(app) # rubocop:disable Metrics/MethodLength
            s = self

            Rack::Builder.new do
              s.each do |prefix, stack|
                s.mapped(self, prefix) do
                  stack.each do |middleware, args, blk|
                    use(middleware, *args, &blk)
                  end
                end

                run app
              end
            end.to_app
          end

          # @since 2.0.0
          # @api private
          def empty?
            @stack.empty?
          end

          # @since 2.0.0
          # @api private
          def each(&blk)
            @stack.each(&blk)
          end

          # @since 2.0.0
          # @api private
          def mapped(builder, prefix, &blk)
            if prefix == ROOT_PREFIX
              builder.instance_eval(&blk)
            else
              builder.map(prefix, &blk)
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hanami-2.0.0.alpha8 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha7.1 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha7 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha6 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha5 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha4 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha3 lib/hanami/application/routing/middleware/stack.rb
hanami-2.0.0.alpha2 lib/hanami/application/routing/middleware/stack.rb