Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions lib/hanami/router/formatter/human_friendly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,51 @@ class HumanFriendly
EXTRA_LARGE_STRING_JUSTIFY_AMOUNT = 40
private_constant :EXTRA_LARGE_STRING_JUSTIFY_AMOUNT

# @api private
# @since 2.0.0
COLUMN_SPACING = 5
private_constant :COLUMN_SPACING

# @api private
# @since 2.0.0
def call(routes)
routes.filter_map(&method(:format_route_unless_head)).join(NEW_LINE)
filtered_routes = routes.reject(&:head?)
return "" if filtered_routes.empty?

column_widths = calculate_column_widths(filtered_routes)
filtered_routes.map { |route| format_route(route, column_widths) }.join(NEW_LINE)
end

private

def format_route_unless_head(route)
!route.head? &&
[
route.http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT),
route.path.ljust(LARGE_STRING_JUSTIFY_AMOUNT),
route.inspect_to.ljust(LARGE_STRING_JUSTIFY_AMOUNT),
route.as? ? "as #{route.inspect_as}".ljust(MEDIUM_STRING_JUSTIFY_AMOUNT) : "",
route.constraints? ? "(#{route.inspect_constraints})".ljust(EXTRA_LARGE_STRING_JUSTIFY_AMOUNT) : ""
].join
def calculate_column_widths(routes)
path_width = LARGE_STRING_JUSTIFY_AMOUNT
inspect_to_width = LARGE_STRING_JUSTIFY_AMOUNT
as_width = MEDIUM_STRING_JUSTIFY_AMOUNT
constraints_width = EXTRA_LARGE_STRING_JUSTIFY_AMOUNT

routes.each do |route|
path_width = [path_width, route.path.length].max
inspect_to_width = [inspect_to_width, route.inspect_to.length].max
as_width = [as_width, route.as? ? "as #{route.inspect_as}".length : 0].max
constraints_width = [constraints_width,
route.constraints? ? "(#{route.inspect_constraints})".length : 0].max
end

# Include spacing in the widths
[path_width + COLUMN_SPACING, inspect_to_width + COLUMN_SPACING, as_width + COLUMN_SPACING, constraints_width]
end

def format_route(route, column_widths)
path_width, inspect_to_width, as_width, constraints_width = column_widths

[
route.http_method.to_s.ljust(SMALL_STRING_JUSTIFY_AMOUNT),
route.path.ljust(path_width),
route.inspect_to.ljust(inspect_to_width),
route.as? ? "as #{route.inspect_as}".ljust(as_width) : "",
route.constraints? ? "(#{route.inspect_constraints})".ljust(constraints_width) : ""
].join
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/hanami/router/inspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

it "inspects the routes" do
expected = [
"GET / (proc) as :root",
"GET /api (proc) as :api_root"
"GET / (proc) as :root ",
"GET /api (proc) as :api_root "
]

actual = inspector.call
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/hanami/router/formatter/human_friendly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Hanami::Router::Route.new(http_method: "GET", path: "/resources/:id", to: "resource#show", as: :resource, constraints: {id: /\d+/})
]

expected = "GET /resources/:id resource#show as :resource (id: /\\d+/) "
expected = "GET /resources/:id resource#show as :resource (id: /\\d+/) "
expect(subject.call(routes)).to eq(expected)
end

Expand Down