Sha256: f41a0dc06dd158319f33148b56d9c6b0fa0316747149f3099e63376e1277c68f

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require "hanami/router"

module Snowpack
  module Web
    class Router < Hanami::Router
      attr_reader :middlewares

      def initialize(application:, **options, &block)
        @application = application
        @options = options
        @middlewares = []

        super(**options, &nil)
        instance_exec(application, &block) if block
        freeze overridden: true
      end

      # Do nothing when the superclass calls freeze during its own initialize
      # (we need to do it later, after our instance_exec of the routes block)
      def freeze(overridden: false)
        super() if overridden
      end

      # Ensure we always return a rack-conformant result (sometimes we get a
      # Hanami::Action::Response here, when we actually want the standard rack
      # 3-element array)
      def call(*)
        super.to_a
      end

      def use(*args, &block)
        middlewares << (args << block)
      end

      def mount(app, at:, host: nil, &block)
        if app.is_a?(Symbol) && (sliced_resolver = @endpoint_resolver.sliced(app))
          sliced_router = self.class.new(
            application: @application.slices[app],
            **@options,
            endpoint_resolver: sliced_resolver,
            &block
          )

          super(sliced_router, at: at, host: host)
        else
          super(app, at: at, host: host)
        end
      end

      # Allow router objects to be mounted within themselves
      def match?(env)
        match_path?(env)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/web/router.rb
snowpack-1.0.0.alpha6 lib/snowpack/web/router.rb
snowpack-1.0.0.alpha5 lib/snowpack/web/router.rb
snowpack-1.0.0.alpha4 lib/snowpack/web/router.rb
snowpack-1.0.0.alpha3 lib/snowpack/web/router.rb
snowpack-1.0.0.alpha2 lib/snowpack/web/router.rb