Roman2K (owner)

Forks

Revisions

gist: 60848 Download_button fork
public
Public Clone URL: git://gist.github.com/60848.git
Embed All Files: show embed
reload_metals.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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?