Gemspec: How can I specify dependencies which don't have to be auto-required?
Asked Answered
R

2

22

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?

Rogelioroger answered 9/10, 2012 at 7:43 Comment(0)
I
16

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.

Inhumanity answered 16/11, 2012 at 7:32 Comment(1)
The official documentation you cite refers to building Rails engines that depend on other gems, not building plain ruby gems. This point is correct: declare gem dependencies of your project in your gemspec using add_dependency. Gems in your project's Gemfile are effectively local dependencies.Symbology
P
0

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.

Photophilous answered 13/11, 2012 at 16:19 Comment(1)
thx for the securerandom tip. About the rest, that was my workaround already, to place it in a Gemfile from the gem. But still, it's a dependency, it should be listed as such, that's still the point of the question. thx for the answer though.Rogelioroger

© 2022 - 2024 — McMap. All rights reserved.