Programmatically check if there is a pearl in the kit?

At runtime, after package configuration is completed, and groups are applied, what is the best way to programmatically check if a given gem_name is in a bundle?

While reading the source code, I found Bundler.definition like

 gem_name = 'banana' Bundler.definition.dependencies.map(&:name).include?(gem_name) 

but the documentation cannot be found 1 I do not know if this use is recommended.

Update: it looks like Bundler::Definition#dependencies returns all dependencies regardless of groups. As an alternative, I found Bundler::Runtime#dependencies_for , which takes groups as arguments.

 Bundler.load.dependencies_for(:default, Rails.env).map(&:name).include?(gem_name) 

However, it seems like a bad idea to duplicate "group lists" on every call site. Ideally, I need a method in the Bundler that does not require me to specify current groups.

1 Vendor web page and man page are command line oriented. I did not find any documentation on the Ruby Gem Public Ruby API. Comments in the source are useful, but oriented to data types, etc.

+6
source share
1 answer

The Bundler is used to configure applications, so you can use the Gem API, not the Bundler :

 Gem.loaded_specs.has_key? gem_name 

The Bundler will set everything so that any gems in the set (in the corresponding groups) are activated (so they will have entries in loaded_specs ), and any other (unrelated) gems will be prevented from being loaded.

+11
source

Source: https://habr.com/ru/post/979719/


All Articles