Last active
March 28, 2016 13:09
-
-
Save noprompt/e365348c60ad19f39102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @return [Array<String>] | |
def gem_paths | |
Gem.path.flat_map { |path| Dir.glob("#{path}/gems/*") } | |
end | |
# @param [String] gem_name | |
# @return [Boolean] | |
# @raise [LoadError] | |
def require_gem(gem_name) | |
if defined?(::Bundler) | |
gem_pattern = Regexp.new(gem_name) | |
matching_paths = gem_paths.grep(gem_pattern) | |
if matching_paths.any? | |
matching_paths.each do |gem_path| | |
$LOAD_PATH.push "#{gem_path}/lib" | |
end | |
begin | |
require(gem_name) | |
rescue LoadError => error | |
# Attempt to load any transitive dependencies. | |
require_gem(error.path) | |
require_gem(gem_name) | |
end | |
else | |
fail LoadError, "Gem #{gem_name} not found." | |
end | |
else | |
require(gem_name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment