Sha256: f552d52ec0d2f715b06e577a55c26a82540c190a6a4a44b531d0a136bf6f6559

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require "hanami/router"
require_relative "routing/middleware/stack"

module Hanami
  class Slice
    # Hanami app router
    # @since 2.0.0
    class Router < ::Hanami::Router
      # @api private
      attr_reader :middleware_stack

      # @since 2.0.0
      # @api private
      def initialize(routes:, middleware_stack: Routing::Middleware::Stack.new, **kwargs, &blk)
        @middleware_stack = middleware_stack
        instance_eval(&blk)
        super(**kwargs, &routes)
      end

      # @since 2.0.0
      # @api private
      def freeze
        return self if frozen?

        remove_instance_variable(:@middleware_stack)
        super
      end

      # @since 2.0.0
      # @api private
      def use(...)
        middleware_stack.use(...)
      end

      # @since 2.0.0
      # @api private
      def scope(*args, &blk)
        middleware_stack.with(args.first) do
          super
        end
      end

      # @since 2.0.0
      def slice(slice_name, at:, &blk)
        blk ||= @resolver.find_slice(slice_name).routes

        prev_resolver = @resolver
        @resolver = @resolver.to_slice(slice_name)

        scope(prefixed_path(at), &blk)
      ensure
        @resolver = prev_resolver
      end

      # @since 2.0.0
      # @api private
      def to_rack_app
        middleware_stack.to_rack_app(self)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hanami-2.0.0.beta4 lib/hanami/slice/router.rb
hanami-2.0.0.beta3 lib/hanami/slice/router.rb
hanami-2.0.0.beta2 lib/hanami/slice/router.rb
hanami-2.0.0.beta1.1 lib/hanami/slice/router.rb
hanami-2.0.0.beta1 lib/hanami/slice/router.rb