Class: Hanami::Router::Route
- Inherits:
-
Object
- Object
- Hanami::Router::Route
- Defined in:
- lib/hanami/router/route.rb
Overview
A route from the router
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the route’s unique name, as given to ‘as:` when the route was defined.
-
#constraints ⇒ Hash
readonly
Returns the route’s contraints hash for its path variables.
-
#http_method ⇒ String
readonly
Returns the route’s HTTP method.
-
#path ⇒ String
readonly
Returns the route’s path.
-
#to ⇒ Object
readonly
Returns the route’s Rack endpoint, as given to ‘to:` when the route was defined.
Instance Method Summary collapse
-
#as? ⇒ Boolean
Returns true if the route has a name.
-
#constraints? ⇒ Boolean
Returns true if the route has any constraints.
-
#head? ⇒ Boolean
Returns true if the route is for the HEAD HTTP method.
-
#initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) ⇒ Route
constructor
private
A new instance of Route.
-
#inspect_as ⇒ String
Returns a string containing a human-readable representation of the route’s name.
-
#inspect_constraints ⇒ String
Returns a string containing a human-readable representation of the route’s #constraints.
-
#inspect_to(value = to) ⇒ String
Returns a string containing a human-readable representation of the route’s #to endpoint.
Constructor Details
#initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) ⇒ Route
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Route.
66 67 68 69 70 71 72 73 74 |
# File 'lib/hanami/router/route.rb', line 66 def initialize(http_method:, path:, to:, as: nil, constraints: {}, blk: nil) # rubocop:disable Metrics/ParameterLists @http_method = http_method @path = path @to = to @as = as @constraints = constraints @blk = blk freeze end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the route’s unique name, as given to ‘as:` when the route was defined.
54 55 56 |
# File 'lib/hanami/router/route.rb', line 54 def as @as end |
#constraints ⇒ Hash (readonly)
Returns the route’s contraints hash for its path variables.
62 63 64 |
# File 'lib/hanami/router/route.rb', line 62 def constraints @constraints end |
#http_method ⇒ String (readonly)
Returns the route’s HTTP method.
27 28 29 |
# File 'lib/hanami/router/route.rb', line 27 def http_method @http_method end |
#path ⇒ String (readonly)
Returns the route’s path.
38 39 40 |
# File 'lib/hanami/router/route.rb', line 38 def path @path end |
#to ⇒ Object (readonly)
Returns the route’s Rack endpoint, as given to ‘to:` when the route was defined.
46 47 48 |
# File 'lib/hanami/router/route.rb', line 46 def to @to end |
Instance Method Details
#as? ⇒ Boolean
Returns true if the route has a name.
96 97 98 |
# File 'lib/hanami/router/route.rb', line 96 def as? !as.nil? end |
#constraints? ⇒ Boolean
Returns true if the route has any constraints.
108 109 110 |
# File 'lib/hanami/router/route.rb', line 108 def constraints? constraints.any? end |
#head? ⇒ Boolean
Returns true if the route is for the HEAD HTTP method.
84 85 86 |
# File 'lib/hanami/router/route.rb', line 84 def head? http_method == ::Rack::HEAD end |
#inspect_as ⇒ String
Returns a string containing a human-readable representation of the route’s name.
155 156 157 |
# File 'lib/hanami/router/route.rb', line 155 def inspect_as as ? as.inspect : Router::EMPTY_STRING end |
#inspect_constraints ⇒ String
Returns a string containing a human-readable representation of the route’s #constraints.
141 142 143 144 145 |
# File 'lib/hanami/router/route.rb', line 141 def inspect_constraints @constraints.map do |key, value| "#{key}: #{value.inspect}" end.join(ROUTE_CONSTRAINT_SEPARATOR) end |
#inspect_to(value = to) ⇒ String
Returns a string containing a human-readable representation of the route’s #to endpoint.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/hanami/router/route.rb', line 118 def inspect_to(value = to) case value when String value when Proc "(proc)" when Class value.name || "(class)" when Block "(block)" when Redirect "#{value.destination} (HTTP #{to.code})" else inspect_to(value.class) end end |