Created
October 27, 2010 01:38
-
-
Save jugyo/648232 to your computer and use it in GitHub Desktop.
force_require for bundler
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
# force_require.rb | |
# | |
# == Usage | |
# | |
# require 'bundler/setup' | |
# require './force_require' | |
# | |
# force_require 'g' | |
# g 'foo' | |
# | |
# require 'g' # => LoadError | |
# | |
module Kernel | |
def new_require(name) | |
begin | |
org_require name | |
rescue LoadError | |
if Thread.current[:force_require_mode] | |
force_require name | |
else | |
raise | |
end | |
end | |
end | |
alias_method :org_require, :require | |
alias_method :require, :new_require | |
def force_require(name) | |
Thread.current[:force_require_mode] = true | |
Gem.all_load_paths.each do |dir| | |
Dir[File.join(dir, "#{name}#{Gem.suffix_pattern}")].each do |file| | |
require file | |
return true | |
end | |
end | |
raise LoadError, "no such file to load -- #{name}" | |
ensure | |
Thread.current[:force_require_mode] = false | |
end | |
alias_method :require!, :force_require | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that TLS = Thread Local Storage.
BTW, I wonder if recursive call should be considered too. doesn't it?
ex)