如何从Sinatra删除路线? [英] How do you remove a route from Sinatra?

查看:80
本文介绍了如何从Sinatra删除路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些动态加载的插件,它们在启动时会注册其路由,但是当它们被禁用时,我还需要能够删除其路由。有没有办法删除现有路由?

I have some dynamically loaded plugins which register their routes when they start up, however I also need to be able to remove their routes when they are disabled. Is there a way to remove existing routes?

API没有我可以找到的删除所有方法的方法,这是我可以想到的唯一方法将直接转到Sinatra :: Base中的@routes对象,但是我不确定您是否可以对此做任何事情,并且如果可以...是否安全?

The API didn't have any methods I could find to remove them, and the only other way I could think to do it would be to go right to the @routes object in Sinatra::Base, but I am not sure if you can do anything to that, and if you can... is it safe to do?

推荐答案

在代码中浏览了几分钟,我看不到任何破坏性地改变路线

Looking through the code for a few minutes I do not see any code that 'destructively' mutates the routes other than:

C:\Ruby\lib\ruby\gems\1.9.1\gems\sinatra-1.3.1\lib\sinatra\base.rb:
  936        def reset!
  937          @conditions     = []
  938:         @routes         = {}
  939          @filters        = {:before => [], :after => []}
  940          @errors         = {}

这是从轨道上驱使它的方法,大概不是您所需要的。根据此调查,我认为您需要自己修改路线哈希。

This is a 'nuke it from orbit' approach, and presumably not what you need. Based on this investigation, I think you will need to modify the routes hash yourself.

对于当前版本的代码,对于 route! 方法总是查找当前的路由数组并正常进行迭代(没有缓存):

For the current version of code, this looks 'safe' to me, insofar as the route! method always looks up the current array of routes and iterates them normally (there is no caching):

def route!(base = settings, pass_block=nil)
  if routes = base.routes[@request.request_method]
    routes.each do |pattern, keys, conditions, block|
      pass_block = process_route(pattern, keys, conditions) do |*args|
        route_eval { block[*args] }
      end
    end
  end

  # Run routes defined in superclass.
  if base.superclass.respond_to?(:routes)
    return route!(base.superclass, pass_block)
  end

  route_eval(&pass_block) if pass_block
  route_missing
end

最近,Sinatra内部存储路线已发生变化发行版,因此如果不对每个新发行版进行测试,我将永远不会依赖于此。更好的是,提出一个补丁,看看是否可以将接受的功能合并到主库中。

The internals of Sinatra around storing routes have shifted in recent releases, so I wouldn't rely on this always working without testing with each new release. Better yet, propose a patch and see if you can get the functionality accepted incorporated into the main library.

这篇关于如何从Sinatra删除路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆