Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

ruby - How to resolve Rails model namespace collision

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 repository
sudo gem install cucumber from my local repository

Now I have two gems to maintain, but that seems easier than changing all the model references in my app.

Comments/suggestions welcome.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...