lib/hanami/router.rb in hanami-router-2.0.0.alpha4 vs lib/hanami/router.rb in hanami-router-2.0.0.alpha5

- old
+ new

@@ -13,18 +13,25 @@ require "hanami/router/redirect" require "hanami/router/prefix" require "hanami/router/params" require "hanami/router/trie" require "hanami/router/block" + require "hanami/router/route" require "hanami/router/url_helpers" # URL helpers for other Hanami integrations # # @api private # @since 2.0.0 attr_reader :url_helpers + # Routes for inspection + # + # @api private + # @since 2.0.0 + attr_reader :routes + # Returns the given block as it is. # # @param blk [Proc] a set of route definitions # # @return [Proc] the given block @@ -59,22 +66,25 @@ # require "hanami/router" # # Hanami::Router.new do # get "/", to: ->(*) { [200, {}, ["OK"]] } # end - def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_found: NOT_FOUND, block_context: nil, &blk) # rubocop:disable Layout/LineLength + def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_found: NOT_FOUND, block_context: nil, inspector: nil, &blk) # rubocop:disable Layout/LineLength # TODO: verify if Prefix can handle both name and path prefix @path_prefix = Prefix.new(prefix) @name_prefix = Prefix.new("") @url_helpers = UrlHelpers.new(base_url) + @base_url = base_url @resolver = resolver @not_found = not_found @block_context = block_context @fixed = {} @variable = {} @globbed = {} @mounted = {} + @blk = blk + @inspector = inspector instance_eval(&blk) if blk end # Resolve the given Rack env to a registered endpoint and invokes it. # @@ -405,11 +415,15 @@ # mount MyRackApp.new, at: "/foo" # end def mount(app, at:, **constraints) path = prefixed_path(at) prefix = Segment.fabricate(path, **constraints) + @mounted[prefix] = @resolver.call(path, app) + if inspect? + @inspector.add_route(Route.new(http_method: "*", path: at, to: app, constraints: constraints)) + end end # Generate an relative URL for a specified named route. # The additional arguments will be used to compose the relative URL - in # case it has tokens to match - and for compose the query string. @@ -586,12 +600,27 @@ RecognizedRoute.new( endpoint, _params(env, params) ) end + # Returns formatted routes + # + # @return [String] formatted routes + # # @since 2.0.0 # @api private + def to_inspect + require "hanami/router/inspector" + + inspector = Inspector.new + with(inspector: inspector) + + inspector.call + end + + # @since 2.0.0 + # @api private def fixed(env) @fixed.dig(env["REQUEST_METHOD"], env["PATH_INFO"]) end # @since 2.0.0 @@ -726,10 +755,16 @@ else add_fixed_route(http_method, path, to) end add_named_route(path, as, constraints) if as + + if inspect? + @inspector.add_route( + Route.new(http_method: http_method, path: path, to: to, as: as, constraints: constraints, blk: blk) + ) + end end # @since 2.0.0 # @api private def resolve_endpoint(path, to, blk) @@ -778,28 +813,55 @@ /\*/.match?(path) end # @since 2.0.0 # @api private + def inspect? + !@inspector.nil? + end + + # @since 2.0.0 + # @api private def prefixed_path(path) @path_prefix.join(path).to_s end # @since 2.0.0 # @api private def prefixed_name(name) @name_prefix.relative_join(name, "_").to_sym end + # Returns a new instance of Hanami::Router with the modified options. + # + # @return [Hanami::Route] a new instance of Hanami::Router + # + # @see Hanami::Router#initialize + # # @since 2.0.0 # @api private + def with(**new_options, &blk) + options = { + base_url: @base_url, + prefix: @path_prefix.to_s, + resolver: @resolver, + not_found: @not_found, + block_context: @block_context, + inspector: @inspector + } + + self.class.new(**options.merge(new_options), &(blk || @blk)) + end + + # @since 2.0.0 + # @api private def _redirect(to, code) body = Rack::Utils::HTTP_STATUS_CODES.fetch(code) do raise UnknownHTTPStatusCodeError.new(code) end destination = prefixed_path(to) - Redirect.new(destination, ->(*) { [code, {"Location" => destination}, [body]] }) + Redirect.new(destination, code, ->(*) { [code, {"Location" => destination}, [body]] }) end # @since 2.0.0 # @api private def _params(env, params)