views:

33

answers:

1

I'm about to extract major functionality of a larger project into a ruby gem.

The little framework I created uses a few additional gems, for different import/export options. Ie.

  • FasterCSV (for ruby 1.8) for csv import/export
  • Nokogiri for csv import/export
  • GraphViz for graph ...
  • PDF
  • ...

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

Is that possible at all?

A: 

The code in your initializer is just code... you could have your gem-user pass through a set of config options, and make the config.gem dependencies only load if those options are present. one way to do this would be to get them to set up global-values in config environemtn eg in config/environment.rb:

CSV_EXPORTS = XML_EXPORTS = true
PDF_EXPORTS = false

Then in your own gem, you'd 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 that if they don't set up any at all, the gems won't try to load. It also means you can default them to whatever you like.

Taryn East