Ruby 已经在使用我的模型的类名
我正在制作一个具有各种授权级别的论坛应用程序,其中之一是监视器。我通过扩展我的 User 类来做到这一点,并且我计划使用“-ship”类(例如管理员身份、作者身份、主持人身份等)对其进行微调。显然,Monitor 类是 ruby mixin 的一部分。如何保留我的资源名称而不发生冲突?
I'm making a forum application with various levels of authorization, one of which is a Monitor. I am doing this by extending my User class, and I plan on fine tuning this with "-ship" classes (e.g. administratorship, authorship, moderatorship, etc.). Apparently the Monitor class is part of ruby mixin. How do I keep my resource name without the collisions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些可能性:
require 'monitor.rb'
引入 Monitor 的空实现。但在所有情况下,您最终可能会遇到这样的情况:第三方库正在使用 Monitor,并期望它成为标准 Monitor 类。因此,我建议不要采取上述任何一种做法。
我想说你唯一的两个合理的选择是:
A)你可以将你的类放在命名空间中:
如果你的应用程序使用某种自动需要魔法(例如它是一个rails应用程序)那么你可以将你的实现放在 /my_app/ 中监视器.rb。当您想引用该类时,您可以执行诸如
my_monitor = MyApp::Monitor.new()
之类的操作。B)你可以使用不同的类名:)
Some possibilities:
require 'monitor.rb'
call which is pulling in the standard Monitor instancerequire 'monitor.rb'
pulls in an empty implementation of Monitor.But in all cases you could end up with the situation where a 3rd party library is using Monitor expecting it to be the standard Monitor class. So, I'd advise against any of the above.
I'd say your only two reasonable options are:
A) you could put your class in a namespace:
if your app uses some kind of auto-require magic (e.g it's a rails app) then you would put your implementation in /my_app/monitor.rb. When you wanted to refer to that class you would do something like
my_monitor = MyApp::Monitor.new()
, or whatever.B) you could use a different class name :)
在其他模块中声明您的 Monitor 类。
Declare your Monitor class in other module.
仅供参考,我刚刚找到了一个巧妙的技巧(呃,黑客)来解决这个问题,这可能会起作用。
我正在开发一个大型遗留应用程序,不幸的是,它有一个非常重要并且到处都在使用的“Fixture”模型。运行测试时,由于 ActiveRecord 在运行测试时使用了 Fixture 类,因此无法创建 Fixture 实例。所以我做了以下操作:
这将我的类冻结在适当的位置,以便我可以稍后引用它(但只是在我的测试中!),而无需由 ActiveRecord Fixture 类(没有命名空间)扩展
FYI, I just found a neat trick (errr, hack) to get around this which may work.
I work on a large legacy application which, unfortunately, has a "Fixture" model which is quite important and which is used everywhere. When running tests, it's impossible to create a Fixture instance because of the Fixture class used by ActiveRecord when running tests. So I did the following:
This freezes my class in place so that I can refer to it later (but just in my tests!) without being extended by the ActiveRecord Fixture class (which is not namespaced)