lib/hanami/router/route.rb in hanami-router-2.0.0.alpha6 vs lib/hanami/router/route.rb in hanami-router-2.0.0.beta1
- old
+ new
@@ -3,28 +3,31 @@
require "hanami/router/redirect"
require "hanami/router/block"
module Hanami
class Router
+ # A route from the router
+ #
+ # @since 2.0.0
class Route
# @api private
# @since 2.0.0
+ ROUTE_CONSTRAINT_SEPARATOR = ", "
+ private_constant :ROUTE_CONSTRAINT_SEPARATOR
+
+ # @since 2.0.0
attr_reader :http_method
- # @api private
# @since 2.0.0
attr_reader :path
- # @api private
# @since 2.0.0
attr_reader :to
- # @api private
# @since 2.0.0
attr_reader :as
- # @api private
# @since 2.0.0
attr_reader :constraints
# @api private
# @since 2.0.0
@@ -36,95 +39,52 @@
@constraints = constraints
@blk = blk
freeze
end
- # @api private
# @since 2.0.0
- def to_inspect
- return EMPTY_ROUTE if head?
-
- result = http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT)
- result += path.ljust(LARGE_STRING_JUSTIFY_AMOUNT)
- result += inspect_to(to).ljust(LARGE_STRING_JUSTIFY_AMOUNT)
- result += "as #{as.inspect}".ljust(MEDIUM_STRING_JUSTIFY_AMOUNT) if as
-
- if constraints?
- result += "(#{inspect_constraints(constraints)})".ljust(EXTRA_LARGE_STRING_JUSTIFY_AMOUNT)
- end
-
- result
+ def head?
+ http_method == ::Rack::HEAD
end
- private
-
- # @api private
# @since 2.0.0
- EMPTY_ROUTE = ""
- private_constant :EMPTY_ROUTE
-
- # @api private
- # @since 2.0.0
- ROUTE_CONSTRAINT_SEPARATOR = ", "
- private_constant :ROUTE_CONSTRAINT_SEPARATOR
-
- # @api private
- # @since 2.0.0
- SMALL_STRING_JUSTIFY_AMOUNT = 8
- private_constant :SMALL_STRING_JUSTIFY_AMOUNT
-
- # @api private
- # @since 2.0.0
- MEDIUM_STRING_JUSTIFY_AMOUNT = 20
- private_constant :MEDIUM_STRING_JUSTIFY_AMOUNT
-
- # @api private
- # @since 2.0.0
- LARGE_STRING_JUSTIFY_AMOUNT = 30
- private_constant :LARGE_STRING_JUSTIFY_AMOUNT
-
- # @api private
- # @since 2.0.0
- EXTRA_LARGE_STRING_JUSTIFY_AMOUNT = 40
- private_constant :EXTRA_LARGE_STRING_JUSTIFY_AMOUNT
-
- # @api private
- # @since 2.0.0
- def head?
- http_method == "HEAD"
+ def as?
+ !as.nil?
end
- # @api private
# @since 2.0.0
def constraints?
constraints.any?
end
- # @api private
# @since 2.0.0
- def inspect_to(to)
- case to
+ def inspect_to(value = to)
+ case value
when String
- to
+ value
when Proc
"(proc)"
when Class
- to.name || "(class)"
+ value.name || "(class)"
when Block
"(block)"
when Redirect
- "#{to.destination} (HTTP #{to.code})"
+ "#{value.destination} (HTTP #{to.code})"
else
- inspect_to(to.class)
+ inspect_to(value.class)
end
end
- # @api private
# @since 2.0.0
- def inspect_constraints(constraints)
- constraints.map do |key, value|
+ def inspect_constraints
+ @constraints.map do |key, value|
"#{key}: #{value.inspect}"
end.join(ROUTE_CONSTRAINT_SEPARATOR)
+ end
+
+ # @since 2.0.0
+ def inspect_as
+ as ? as.inspect : Router::EMPTY_STRING
end
end
end
end