在 Rails 中,我查看我通过以下方式定义的所有路线:
rake 动路线
是否有内置方法可以在 Roda 应用程序中执行此操作?
如果没有,Roda 开发人员是否使用通用模式来快速查看所有路线(可能是某种 Rake 任务?)
最佳答案
不可能自动。 Roda 路由在收到请求时动态评估,因此它们不会提前加载并存储在某处可用的数据结构中。
作为documentation说:
The route block is called whenever a new request comes in. It is yielded an instance of a subclass of Rack::Request with some additional methods for matching routes.
一个简单的解决方案,需要最少的努力,是使用 roda-route_list
插件,但它需要在 app.rb
文件中每个路由的顶部添加解释性注释,如下所示:
# route: /path/to/foo
# route: GET /path/to/foo
# ...
(检查documentation以了解其他可能性)
然后您必须创建一个包含路由元数据的 json 文件,您可以通过启动 roda-route_list
插件附带的脚本来执行此操作
roda-parse_routes -f routes.json app.rb
它会在应用程序的根目录中创建文件routes.json
,最后您可以使用以下命令列出路由:
route_list # => [{:path=>'/path/to/foo', :methods=>['GET', 'POST']}]
# (it is an Array of route metadata hashes)
您还可以创建一个简单的 rake 任务来列出所有路由,如下所示:
# Rakefile
require 'roda'
namespace :routes do
task :list do |task|
class App < Roda
plugin :route_list
puts route_list
end
end
end
也许有比这个片段更优雅的解决方案,但它有效:)
希望对你有帮助!
关于ruby - 列出 Roda 应用程序中的所有路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542890/