Class: Lotus::Routing::RoutesInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/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

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Lotus::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



27
28
29
# File 'lib/lotus/routing/routes_inspector.rb', line 27

def initialize(routes)
  @routes = routes
end

Instance Method Details

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

Return a formatted string that describes all the routes

Examples:

Default formatter

require 'lotus/router'

router = Lotus::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
  # =>        GET, HEAD  /                        Home::Index
       login  GET, HEAD  /login                   Sessions::New
              POST       /login                   Sessions::Create
       logout GET, HEAD  /logout                  Sessions::Destroy

Custom formatter

require 'lotus/router'

router = Lotus::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)
  # => | GET, HEAD |        | /       | Home::Index       |
       | GET, HEAD | login  | /login  | Sessions::New     |
       | POST      |        | /login  | Sessions::Create  |
       | GET, HEAD | logout | /logout | Sessions::Destroy |

Nested routes

require 'lotus/router'

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

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

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

puts router.inspector.to_s(formatter)
  # => | 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: nil)

    the base path of a nested route

Returns:

  • (String)

    routes pretty print

See Also:

Since:

  • 0.2.0



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lotus/routing/routes_inspector.rb', line 107

def to_s(formatter = FORMATTER, base_path = nil)
  base_path = Utils::PathPrefix.new(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