Class: Hanami::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/router/route.rb

Overview

A route from the router

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 2.0.0



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

#asObject (readonly)

Returns the route’s unique name, as given to ‘as:` when the route was defined.

Returns:

  • (Object)

Since:

  • 2.0.0



54
55
56
# File 'lib/hanami/router/route.rb', line 54

def as
  @as
end

#constraintsHash (readonly)

Returns the route’s contraints hash for its path variables.

Returns:

  • (Hash)

Since:

  • 2.0.0



62
63
64
# File 'lib/hanami/router/route.rb', line 62

def constraints
  @constraints
end

#http_methodString (readonly)

Returns the route’s HTTP method.

Examples:

route.http_method # => "GET"

Returns:

  • (String)

Since:

  • 2.0.0



27
28
29
# File 'lib/hanami/router/route.rb', line 27

def http_method
  @http_method
end

#pathString (readonly)

Returns the route’s path.

Examples:

route.path # => "/a/b/c"

Returns:

  • (String)

Since:

  • 2.0.0



38
39
40
# File 'lib/hanami/router/route.rb', line 38

def path
  @path
end

#toObject (readonly)

Returns the route’s Rack endpoint, as given to ‘to:` when the route was defined.

Returns:

  • (Object)

Since:

  • 2.0.0



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.

Returns:

  • (Boolean)

See Also:

Since:

  • 2.0.0



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.

Returns:

  • (Boolean)

See Also:

Since:

  • 2.0.0



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.

Returns:

  • (Boolean)

See Also:

Since:

  • 2.0.0



84
85
86
# File 'lib/hanami/router/route.rb', line 84

def head?
  http_method == ::Rack::HEAD
end

#inspect_asString

Returns a string containing a human-readable representation of the route’s name.

Returns:

  • (String)

See Also:

Since:

  • 2.0.0



155
156
157
# File 'lib/hanami/router/route.rb', line 155

def inspect_as
  as ? as.inspect : Router::EMPTY_STRING
end

#inspect_constraintsString

Returns a string containing a human-readable representation of the route’s #constraints.

Returns:

  • (String)

Since:

  • 2.0.0



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.

Returns:

  • (String)

Since:

  • 2.0.0



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