Question

I wrote a gem with a certain array of dependencies, and some of them I'd like not to have implicitly required when bundled into another project. An example is the uuidtools gem, which I only want to require in files using it.

gem.add_dependency("uuidtools",["=2.1.3"], :require => false)

This syntax is false, since :require => false is unexpected there, but this more or less sums up what I would like to do with it. Can someone help me on this?

Was it helpful?

Solution

Gems specified in an engines gemspec file already do need to be explicitly required, by default. From the official documentation - Note that if you want to immediately require dependencies when the engine is required, you should require them before the engine's initialization. In your case, you should be able to get by with something like gem.add_dependency 'uuidtools', '2.1.3' in your gemspec file, and requires in the relevant locations.

OTHER TIPS

I think the way to accomplish what you're asking is don't put it in your gemspec proper but instead add a Gemfile for bundler. Then you can add it as a bundler installed gem.

To do this add the simple word "gemspec" at top of the Gemfile, or after the source declarations. This will pick up the gemspec specific gems. This is basically not good design though. It seems more like entropy which you should avoid in gems and source code when possible. Having said that I don't think there is any harm installing the gem and having it required. It should be namespaced properly and won't interfere with anything else.

Given the gem you cite. You may not even need that gem anymore. Are you on Ruby 1.9? If so, there is now the SecureRandom module built into Ruby now.

require 'secure_random'

my_uuid = SecureRandom.uuid

You can generate a true UUID with it too! So your DBAs will be happy and can use the UUID datatype in Postgres. (You are using postgres, right! haha). Also IIRC, UUIDTools doesn't generate a proper UUID according to the standards. I believe the 3rd sequence is wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top