Ruby Gemspec中的条件依赖项 [英] Conditional Dependency in Ruby Gemspec

查看:151
本文介绍了Ruby Gemspec中的条件依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要安装json gem才能工作的gem。无论哪个json gem:json_pure,json-jruby或基于C的json。

有没有在gemspec中定义这个好方法? 此回复建议为每个版本保留一个完全独立的gem,但似乎必须有一个更好的方式。

有没有人有这方面的经验?



我应该只使用 spec.requirements 选项可以让用户注意到他/她需要一个json宝石?

解决方案

是的,我建议在 spec.requirements 。在gem第一次加载时,我还会推荐一些加载链接:

 #init.rb和/或rails / init.rb:
除非Object.const_defined?(:JSON)
begin
require'json_pure'
rescue LoadError
begin
require'json-ruby '
rescue LoadError
require'json'
end
end
end
除非Object.const_defined?(:JSON)
raise可能不加载gem MyGem;你安装了json_pur,json-ruby还是基于C的json库?
end


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?

解决方案

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

这篇关于Ruby Gemspec中的条件依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆