lib/hanami/routing/endpoint_resolver.rb in hanami-router-1.3.2 vs lib/hanami/routing/endpoint_resolver.rb in hanami-router-2.0.0.alpha1

- old
+ new

@@ -1,24 +1,26 @@ -require 'hanami/utils/string' -require 'hanami/utils/class' -require 'hanami/routing/endpoint' +# frozen_string_literal: true +require "hanami/utils/string" +require "hanami/utils/class" +require "hanami/routing/endpoint" + module Hanami module Routing # Resolve duck-typed endpoints # # @since 0.1.0 # # @api private class EndpointResolver # @since 0.2.0 # @api private - NAMING_PATTERN = '%{controller}::%{action}'.freeze + NAMING_PATTERN = "%<controller>s::%<action>s" # @since 0.7.0 # @api private - DEFAULT_RESPONSE = [404, {'X-Cascade' => 'pass'}, 'Not Found'].freeze + DEFAULT_RESPONSE = [404, { "X-Cascade" => "pass" }, "Not Found"].freeze # Default separator for controller and action. # A different separator can be passed to #initialize with the `:separator` option. # # @see #initialize @@ -30,11 +32,11 @@ # require 'hanami/router' # # router = Hanami::Router.new do # get '/', to: 'articles#show' # end - ACTION_SEPARATOR = '#'.freeze + ACTION_SEPARATOR = "#" attr_reader :action_separator # Initialize an endpoint resolver # @@ -181,14 +183,15 @@ def find(options) options[:to] end protected + # @api private def default @endpoint_class.new( - ->(env) { DEFAULT_RESPONSE } + ->(_env) { DEFAULT_RESPONSE } ) end # @api private def constantize(string) @@ -206,10 +209,11 @@ def classify(string) Utils::String.transform(string, :underscore, :classify) end private + # @api private def resolve_callable(callable) if callable.respond_to?(:call) @endpoint_class.new(callable) elsif callable.is_a?(Class) && callable.instance_methods.include?(:call) @@ -217,22 +221,22 @@ end end # @api private def resolve_matchable(matchable) - if matchable.respond_to?(:match) - constantize( - resolve_action(matchable) || classify(matchable) - ) - end + return unless matchable.respond_to?(:match) + + constantize( + resolve_action(matchable) || classify(matchable) + ) end # @api private def resolve_action(string) - if string.match(action_separator) - controller, action = string.split(action_separator).map {|token| classify(token) } - @pattern % {controller: controller, action: action} - end + return unless string.match?(action_separator) + + controller, action = string.split(action_separator).map { |token| classify(token) } + format(@pattern, controller: controller, action: action) end end end end