我有一个宝石:如何让Rails停止重新加载我的宝石?
# in /Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib/my_gem.rb
module MyGem
def do_stuff
..
end
end
而且我装它的Rails:
# in [rails_root]/config/environment.rb:
config.gem 'my_gem', :version => '1.0.0'
,并用它:
# in [rails_root]/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include MyGem
end
但我需要猴子补丁有点在特定环境的方式:
# in [rails_root]/config/environments/development.rb:
MyGem.class_eval do
def do_stuff
raise 'Ack! - just testing'
end
end
不幸的是,MyGem
重新加载每个请求,所以我的猴子补丁是无用的。
我检查load_once_paths
:
ActiveSupport::Dependencies.load_once_paths
# => ["/Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib", "other stuff"]
任何想法我如何能得到我想要的效果?
我正在_requiring_它(隐式地)在environment.rb;我在ApplicationController中_including_它。这些是非常不同的行为。我想知道的是为什么Rails每次重新加载ApplicationController时都会重新加载gem。证明它这样做是因为我的猴子补丁消失了。 – 2009-07-23 12:17:30