Skip to content

Instantly share code, notes, and snippets.

@Roman2K
Created February 9, 2009 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Roman2K/60848 to your computer and use it in GitHub Desktop.
Save Roman2K/60848 to your computer and use it in GitHub Desktop.
# Portable monkey patch for reloading constants before hitting metal
# applications. More details in the introduction article at:
#
# http://roman.flucti.com/reloading-rails-metal-applications
#
Rails::Rack::Metal.class_eval do
# Prevent Metal from further require'ing source files, in order to let
# AS::Dependencies handle constants.
class << self
def require(*args)
end
end
# Remove already loaded metal constants so that they get loaded by
# AS::Dependencies the next time they are needed.
metals.grep(Module).each do |mod|
$".delete(mod.name.underscore + ".rb")
ActiveSupport::Dependencies.remove_constant(mod.name)
end
# Force metal constants to be reloaded before each request.
def call_with_reload(*args, &block)
@metals.clear
RELOADER.reload do
self.class.metals.each { |app| @metals[app] = true }
call_without_reload(*args, &block)
end
end
alias_method_chain :call, 'reload'
# Expose the reloading mechanism via a stripped down Dispatcher instance.
bare_dispatcher = Class.new(ActionController::Dispatcher) do
def initialize
end
def reload
reload_application
begin
yield
ensure
cleanup_application if respond_to? :cleanup_application
end
end
end
RELOADER = bare_dispatcher.new
end if Rails.env.development?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment