开发者

Ruby Gems with flexible gem dependencies?

I'm about to extract major functionalit开发者_开发知识库y 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜