Conditional dependency in Ruby Gemspec

I am creating a gem that needs json gem for it to work. It doesn't matter which json gem is: json_pure, json-jruby or json.

Is there a good way to define this in gemspec? This answer suggests keeping a completely separate stone for each version, but it seems that the best way was.

Does anyone have any experience?

Should I use the spec.requirements parameter to give the user a notification that he / she needs a json gem?

+7
ruby rubygems
source share
1 answer

Yes, I would suggest a simple text requirement in spec.requirements . I would also recommend some kind of load snapping when the ground is first loaded:

 # in init.rb and/or rails/init.rb: unless Object.const_defined?(:JSON) begin require 'json_pure' rescue LoadError begin require 'json-ruby' rescue LoadError require 'json' end end end unless Object.const_defined?(:JSON) raise "Could not load gem MyGem; did you install one of json_pur, json-ruby, or the C-based json library?" end 
+2
source share

All Articles