46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/rage/cli.rb', line 46
def routes
return help("routes") if options.help?
set_env(options)
environment
routes = Rage.__router.routes
pattern = options[:grep]
routes.unshift({ method: "Verb", path: "Path", meta: { raw_handler: "Controller#Action" } })
grouped_routes = routes.each_with_object({}) do |route, memo|
if pattern && !memo.empty?
next unless route[:path].match?(pattern) || route[:meta][:raw_handler].to_s.match?(pattern) || route[:method].match?(pattern)
end
key = [route[:path], route[:meta][:raw_handler]]
if route[:meta][:mount]
memo[key] = route.merge(method: "") unless route[:path].end_with?("*")
next
end
if memo[key]
memo[key][:method] += "|#{route[:method]}"
else
memo[key] = route
end
end
longest_path = longest_method = 0
grouped_routes.each do |_, route|
longest_path = route[:path].length if route[:path].length > longest_path
longest_method = route[:method].length if route[:method].length > longest_method
end
margin = 3
longest_path += margin
longest_method += margin
grouped_routes.each_with_index do |(_, route), i|
meta = route[:constraints]
meta.merge!(route[:defaults]) if route[:defaults]
handler = route[:meta][:raw_handler]
handler = "#{handler} #{meta}" unless meta&.empty?
puts format("%-#{longest_method}s%-#{longest_path}s%s", route[:method], route[:path], handler)
puts "\n" if i == 0
end
end
|