Class: Hanami::Routing::RoutesInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/routing/routes_inspector.rb

Overview

Routes inspector

Since:

  • 0.2.0

Constant Summary collapse

FORMATTER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default route formatter

Since:

  • 0.2.0

"%<name>20s %<methods>-10s %<path>-30s %<endpoint>-30s\n".freeze
HTTP_METHODS_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default HTTP methods separator

Since:

  • 0.2.0

', '.freeze
INSPECTOR_HEADER_HASH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default inspector header hash values

Since:

  • 0.5.0

Hash[
  name:     'Name',
  methods:  'Method',
  path:     'Path',
  endpoint: 'Action'
].freeze
INSPECTOR_HEADER_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default inspector header name values

Since:

  • 0.5.0

'Name'.freeze
EMPTY_LINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Empty line string

Since:

  • 0.5.0

"\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(routes, prefix) ⇒ Hanami::Routing::RoutesInspector

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.

Instantiate a new inspector

Since:

  • 0.2.0



50
51
52
53
# File 'lib/hanami/routing/routes_inspector.rb', line 50

def initialize(routes, prefix)
  @routes = routes
  @prefix = prefix
end

Instance Method Details

#inspect_routes(formatter, base_path) ⇒ String

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 string representation of routes

Parameters:

  • formatter (String)

    the template for the output

  • base_path (Hanami::Utils::PathPrefix)

    the base path

Returns:

  • (String)

    serialized routes from router

See Also:

Since:

  • 0.5.1



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hanami/routing/routes_inspector.rb', line 157

def inspect_routes(formatter, base_path)
  result = ''

  # TODO refactoring: replace conditional with polymorphism
  # We're exposing too much knowledge from Routing::Route:
  # #path_for_generation and #base_path
  @routes.each do |route|
    result << if router = route.nested_router
      inspect_router(formatter, router, route, base_path)
    else
      inspect_route(formatter, route, base_path)
    end
  end

  result
end

#to_s(formatter = FORMATTER, base_path = prefix) ⇒ String

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.

Return a formatted string that describes all the routes

Examples:

Default formatter

require 'hanami/router'

router = Hanami::Router.new do
  get    '/',       to: 'home#index'
  get    '/login',  to: 'sessions#new',     as: :login
  post   '/login',  to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy', as: :logout
end

puts router.inspector.to_s
  # =>   Name Method     Path                     Action

              GET, HEAD  /                        Home::Index
        login GET, HEAD  /login                   Sessions::New
              POST       /login                   Sessions::Create
       logout GET, HEAD  /logout                  Sessions::Destroy

Custom formatter

require 'hanami/router'

router = Hanami::Router.new do
  get    '/',       to: 'home#index'
  get    '/login',  to: 'sessions#new',     as: :login
  post   '/login',  to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy', as: :logout
end

formatter = "| %{methods} | %{name} | %{path} | %{endpoint} |\n"

puts router.inspector.to_s(formatter)
  # => | Method    | Name   | Path    | Action            |

       | GET, HEAD |        | /       | Home::Index       |
       | GET, HEAD | login  | /login  | Sessions::New     |
       | POST      |        | /login  | Sessions::Create  |
       | GET, HEAD | logout | /logout | Sessions::Destroy |

Nested routes

require 'hanami/router'

class AdminHanamiApp
  def call(env)
  end
  def routes
    Hanami::Router.new {
      get '/home', to: 'home#index'
    }
  end
end

router = Hanami::Router.new {
  get '/fakeroute', to: 'fake#index'
  mount AdminHanamiApp, at: '/admin'
  mount Hanami::Router.new {
    get '/posts', to: 'posts#index'
    mount Hanami::Router.new {
      get '/comments', to: 'comments#index'
    }, at: '/second_mount'
  }, at: '/api'
}

formatter = "| %{methods} | %{name} | %{path} | %{endpoint} |\n"

puts router.inspector.to_s(formatter)
  # => | Method    | Name | Path                       | Action          |

       | GET, HEAD |      | /fakeroute                 | Fake::Index     |
       | GET, HEAD |      | /admin/home                | Home::Index     |
       | GET, HEAD |      | /api/posts                 | Posts::Index    |
       | GET, HEAD |      | /api/second_mount/comments | Comments::Index |

Parameters:

  • formatter (String) (defaults to: FORMATTER)

    the optional formatter for a route

  • base_path (String) (defaults to: prefix)

    the base path of a nested route

Returns:

  • (String)

    routes pretty print

See Also:

Since:

  • 0.2.0



138
139
140
141
142
143
# File 'lib/hanami/routing/routes_inspector.rb', line 138

def to_s(formatter = FORMATTER, base_path = prefix)
  base_path = Utils::PathPrefix.new(base_path)

  inspect_routes(formatter, base_path)
    .insert(0, formatter % INSPECTOR_HEADER_HASH + EMPTY_LINE)
end