Skip to content

Commit

Permalink
Merge pull request #78 from davydovanton/display-routes-header
Browse files Browse the repository at this point in the history
Add option for display header text for routes inspector
  • Loading branch information
jodosha committed Dec 20, 2015
2 parents 39d4a80 + 844b88e commit 39b3337
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lib/lotus/routing/routes_inspector.rb
Expand Up @@ -18,6 +18,29 @@ class RoutesInspector
# @api private
HTTP_METHODS_SEPARATOR = ', '.freeze

# Default inspector header hash values
#
# @since 0.5.0
# @api private
INSPECTOR_HEADER_HASH = Hash[
name: 'Name',
methods: 'Method',
path: 'Path',
endpoint: 'Action'
].freeze

# Default inspector header name values
#
# @since 0.5.0
# @api private
INSPECTOR_HEADER_NAME = 'Name'.freeze

# Empty line string
#
# @since 0.5.0
# @api private
EMPTY_LINE = "\n".freeze

# Instantiate a new inspector
#
# @return [Lotus::Routing::RoutesInspector] the new instance
Expand Down Expand Up @@ -50,8 +73,10 @@ def initialize(routes)
# end
#
# puts router.inspector.to_s
# # => GET, HEAD / Home::Index
# login GET, HEAD /login Sessions::New
# # => Name Method Path Action
#
# GET, HEAD / Home::Index
# login GET, HEAD /login Sessions::New
# POST /login Sessions::Create
# logout GET, HEAD /logout Sessions::Destroy
#
Expand All @@ -68,7 +93,9 @@ def initialize(routes)
# formatter = "| %{methods} | %{name} | %{path} | %{endpoint} |\n"
#
# puts router.inspector.to_s(formatter)
# # => | GET, HEAD | | / | Home::Index |
# # => | Method | Name | Path | Action |
#
# | GET, HEAD | | / | Home::Index |
# | GET, HEAD | login | /login | Sessions::New |
# | POST | | /login | Sessions::Create |
# | GET, HEAD | logout | /logout | Sessions::Destroy |
Expand Down Expand Up @@ -100,10 +127,12 @@ def initialize(routes)
# 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 |
# # => | 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 |
def to_s(formatter = FORMATTER, base_path = nil)
base_path = Utils::PathPrefix.new(base_path)
result = ''
Expand All @@ -119,6 +148,10 @@ def to_s(formatter = FORMATTER, base_path = nil)
end
end

unless result.include?(INSPECTOR_HEADER_NAME)
result.insert(0, formatter % INSPECTOR_HEADER_HASH + EMPTY_LINE)
end

result
end

Expand Down
15 changes: 15 additions & 0 deletions test/routing/routes_inspector_test.rb
Expand Up @@ -252,5 +252,20 @@ def routes
end
end
end

describe 'with header option' do
before do
@router = Lotus::Router.new do
get '/controller/action', to: 'welcome#index'
end
end

it 'returns header text' do
expectation = %(Name Method Path Action)

actual = @router.inspector.to_s
actual.must_include(expectation)
end
end
end
end

0 comments on commit 39b3337

Please sign in to comment.