Ruby Gems with Flexible Gem Dependencies?

I am going to extract the main functions of a large project in a ruby ​​stone.

The small structure I created uses a few extra gems for different import / export settings. Those..

  • FasterCSV (for ruby ​​1.8) for import / export of csv
  • Nokogiri for import / export csv
  • GraphViz for graph ...
  • Pdf
  • ...

I don't want gem users to install and download all the gems when they don’t need them.

Is this even possible?

+4
source share
1 answer

The code in your initializer is just code ... you could get your gem user to go through a set of configuration parameters and make config.gem dependencies only if these parameters are present. one way to do this would be to force them to configure global values ​​in config environemtn for example in config / environment.rb:

CSV_EXPORTS = XML_EXPORTS = true PDF_EXPORTS = false 

Then in your own stone you should write:

 config.gem 'fastercsv' if defined?(CSV_EXPORTS) config.gem 'nokogiri' if defined?(XML_EXPORTS) if defined?(PDF_EXPORTS) config.gem 'prawn' config.gem 'prawn-layout' end # etc 

use "defined?" so if they are not configured at all, the gems will not try to load. It also means that you can use them as you wish.

0
source

All Articles