Class Rack::Reloader
In: lib/rack/reloader.rb
Parent: Object

Rack::Reloader checks on every request, but at most every secs seconds, if a file loaded changed, and reloads it, logging to rack.errors.

It is recommended you use ShowExceptions to catch SyntaxErrors etc.

Methods

call   new   reload!  

Public Class methods

[Source]

    # File lib/rack/reloader.rb, line 11
11:     def initialize(app, secs=10)
12:       @app = app
13:       @secs = secs              # reload every @secs seconds max
14:       @last = Time.now
15:     end

Public Instance methods

[Source]

    # File lib/rack/reloader.rb, line 17
17:     def call(env)
18:       if Time.now > @last + @secs
19:         Thread.exclusive {
20:           reload!(env['rack.errors'])
21:           @last = Time.now
22:         }
23:       end
24: 
25:       @app.call(env)
26:     end

[Source]

    # File lib/rack/reloader.rb, line 28
28:     def reload!(stderr=STDERR)
29:       need_reload = $LOADED_FEATURES.find_all { |loaded|
30:         begin
31:           if loaded =~ /\A[.\/]/  # absolute filename or 1.9
32:             abs = loaded
33:           else
34:             abs = $LOAD_PATH.map { |path| ::File.join(path, loaded) }.
35:                              find { |file| ::File.exist? file }
36:           end
37: 
38:           if abs
39:             ::File.mtime(abs) > @last - @secs  rescue false
40:           else
41:             false
42:           end
43:         end
44:       }
45: 
46:       need_reload.each { |l|
47:         $LOADED_FEATURES.delete l
48:       }
49: 
50:       need_reload.each { |to_load|
51:         begin
52:           if require to_load
53:             stderr.puts "#{self.class}: reloaded `#{to_load}'"
54:           end
55:         rescue LoadError, SyntaxError => e
56:           raise e                 # Possibly ShowExceptions
57:         end
58:       }
59: 
60:       stderr.flush
61:       need_reload
62:     end

[Validate]