Forked from skojin/irb_require_without_bundler_hack.rb
Created
April 6, 2011 20:13
-
-
Save amarshall/906427 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
# Loads the specified gem from RVM's current @global gemset if using | |
# Bundler and not included in the current Gemfile. Falls back | |
# accordingly if not using Bundler or RVM. | |
# | |
# @note Doesn't load gems not loaded by Bundler if using Bundler and | |
# not using RVM, or if gem is located somewhere other than the | |
# @global gemset. | |
# @param [String] The gem to require | |
# @raise [LoadError] If the gem couldn't be loaded | |
# Adapted from <https://gist.github.com/794915> | |
def require_global_gem_without_bundler(gem) | |
if defined? ::Bundler | |
# Check if the gem has already been loaded by Bundler | |
unless Bundler.require.map { |i| i.name }.include? gem | |
if global_gem_dir = $LOAD_PATH.grep(/@global.*?lib/).first | |
global_gem_dir = global_gem_dir.gsub %r{@global/gems/.*$}, '@global/gems' | |
Dir["#{global_gem_dir}/*"].to_a.each do |gem_path| | |
if File.basename(gem_path).gsub(/-(\d\.?)+$/, '') == gem | |
$LOAD_PATH << "#{gem_path}/lib" | |
require gem | |
return | |
end | |
end | |
end | |
# Either RVM's @global gemset isn't available, or the gem isn't | |
# installed in it, either way, gem wasn't be loaded. | |
raise LoadError | |
end | |
else | |
# Bundler isn't being used, load gem normally | |
require gem | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment