Ruby 已经在使用我的模型的类名

发布于 2024-08-29 21:56:02 字数 229 浏览 8 评论 0原文

我正在制作一个具有各种授权级别的论坛应用程序,其中之一是监视器。我通过扩展我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴梦一场 2024-09-05 21:56:02

一些可能性:

  • 避免调用标准 Monitor 实例的 require 'monitor.rb' 调用,
  • 执行一些运行时魔法来重命名现有的 Monitor 类。
  • 修改您的加载路径,以便 require 'monitor.rb' 引入 Monitor 的空实现。

但在所有情况下,您最终可能会遇到这样的情况:第三方库正在使用 Monitor,并期望它成为标准 Monitor 类。因此,我建议不要采取上述任何一种做法。

我想说你唯一的两个合理的选择是:

A)你可以将你的类放在命名空间中:

Module MyApp
  class Monitor
    #...
  end
end

如果你的应用程序使用某种自动需要魔法(例如它是一个rails应用程序)那么你可以将你的实现放在 /my_app/ 中监视器.rb。当您想引用该类时,您可以执行诸如 my_monitor = MyApp::Monitor.new() 之类的操作。

B)你可以使用不同的类名:)

Some possibilities:

  • avoid the require 'monitor.rb' call which is pulling in the standard Monitor instance
  • do some runtime magic to rename the existing Monitor class.
  • monkey with your load path so that require '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:

Module MyApp
  class Monitor
    #...
  end
end

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 :)

⊕婉儿 2024-09-05 21:56:02

在其他模块中声明您的 Monitor 类。

module MyModule

  class Monitor

  end

end

Declare your Monitor class in other module.

module MyModule

  class Monitor

  end

end
情何以堪。 2024-09-05 21:56:02

仅供参考,我刚刚找到了一个巧妙的技巧(呃,黑客)来解决这个问题,这可能会起作用。

我正在开发一个大型遗留应用程序,不幸的是,它有一个非常重要并且到处都在使用的“Fixture”模型。运行测试时,由于 ActiveRecord 在运行测试时使用了 Fixture 类,因此无法创建 Fixture 实例。所以我做了以下操作:

FixtureModel = Fixture.dup

这将我的类冻结在适当的位置,以便我可以稍后引用它(但只是在我的测试中!),而无需由 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:

FixtureModel = Fixture.dup

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文