tags:

views:

191

answers:

1

I'm building a gem that needs a json gem installed in order for it to work. It doesn't matter which json gem: json_pure, json-jruby, or the C-based json.

Is there a good way of defining this in a gemspec? This response suggests maintaining a completely separate gem for each version, but it seems like there has got to be a better way.

Does anybody have any experience with this?

Should I just use the spec.requirements option to give the user a notice that he/she needs a json gem?

A: 

Yes, I would suggest a simple text requirement in spec.requirements. I would also recommend some sort of load-chaining when the gem first loads:

# 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
James A. Rosen
Sounds like a good idea. The cool thing with all of the JSON gems is that you just require 'json' so it will simplify the load-chaining.
Jimmy Z