如何解决 Rails 模型命名空间冲突
到目前为止的故事:
我有一个 Rails 应用程序,其模型名为“Term”。一切都很顺利,直到尝试安装 Cucumber。运行时
rake cucumber
我得到
Term is not a class (TypeError)
这种情况,因为 Cucumber 包含另一个 gem,“term-ansicolor”(在控制台中进行漂亮的彩色文本输出),并且 term-ansicolor 定义了一个名为“Term”的模块。 Cucumber 在包含 Rails 模型之前包含 term-ansicolor,因此在加载“Term”模型时“Term”已经被定义为模块。顶级模块和类在 Ruby 中不能具有相同的名称,因此会发生冲突。
由于不想重命名该模型,我开始修补术语 ansicolor gem。事实证明这比我想象的要难。我将 Term 模块名称更改为“ANSITerm”,但我不知道如何让 Cucumber 加载修改后的 gem,我已将其放入 RAILS_ROOT/vendor/gems/term-ansicolor 中。
有什么想法吗?我是不是找错了树?
The story so far:
I have a rails app with a model named "Term". All is well until trying to install Cucumber. Upon running
rake cucumber
I get
Term is not a class (TypeError)
This happens because Cucumber includes another gem, 'term-ansicolor' (to do the nifty colored text output in the console), and term-ansicolor defines a module named "Term". Cucumber includes term-ansicolor before including the Rails models, thus "Term" is already defined as a module when loading the "Term" model. Top-level modules and classes cannot have the same names in Ruby, thus the collision.
Preferring not to rename the model, I set about patching the term-ansicolor gem. This proved harder than I thought. I changed the Term module name to "ANSITerm", but I can't figure out how to get Cucumber to load my modified gem, which I've put into RAILS_ROOT/vendor/gems/term-ansicolor.
Any ideas? Am I barking up the wrong tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我所做的:
sudo gem uninstall term-ansicolor
sudo gem uninstall cucumber
从 github 下载 term-ansicolor 和 cucumber 的源代码
在 term-ansicolor 源中搜索
“module Term”
并替换为“module ANSITerm”
在 Cucumber 源中搜索
"include Term"
并替换为"include ANSITerm"
在黄瓜源中搜索
"::Term"
并替换为"::ANSITerm"
sudo gem install term-ansicolor
从我的本地存储库来自我的本地存储库的 sudo gem install cucumber
现在我有两个 gem 需要维护,但这似乎比更改我的应用程序中的所有模型引用更容易。
欢迎提出意见/建议。
Here's what I did:
sudo gem uninstall term-ansicolor
sudo gem uninstall cucumber
Download sources for term-ansicolor and cucumber from github
Search term-ansicolor source for
"module Term"
and replace with"module ANSITerm"
Search cucumber source for
"include Term"
and replace with"include ANSITerm"
Search cucumber source for
"::Term"
and replace with"::ANSITerm"
sudo gem install term-ansicolor
from my local repositorysudo gem install cucumber
from my local repositoryNow I have two gems to maintain, but that seems easier than changing all the model references in my app.
Comments/suggestions welcome.
两种解决方案:
1) 将应用程序的术语模型更改为其他模型。
2) 修补 term-ansicolor 以拥有命名空间 Term 并使用该 gem 代替。
Two solutions:
1) Change your app's Term model to be something else.
2) Patch term-ansicolor to have a namespaced Term and use that gem instead.
我确实在我的规范中遇到了与我的应用程序模型 Node 和 Capybara::Node 的命名空间冲突。对我来说,在规范中明确声明顶级命名空间就足以解决问题:
I did get a namespace collision with my application model Node and Capybara::Node in my specs. It was sufficient for me to explicitly state toplevel namespace in specs to fix the problem: