diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-04-14 15:52:23 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-04-14 15:52:23 -0500 |
commit | 4839fe2e8201835a53ab6c75528a613e8bf25bfb (patch) | |
tree | 55f5c3f76358fcbb28fca6ac9bcd915cbefac89a /actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb | |
parent | 1120aaceae95b95ef491ca226154500051b5fe38 (diff) | |
download | rails-4839fe2e8201835a53ab6c75528a613e8bf25bfb.tar.gz rails-4839fe2e8201835a53ab6c75528a613e8bf25bfb.tar.bz2 rails-4839fe2e8201835a53ab6c75528a613e8bf25bfb.zip |
Move bundled rack into ActionDispatch
Diffstat (limited to 'actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb b/actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb new file mode 100644 index 0000000000..b17d8c0926 --- /dev/null +++ b/actionpack/lib/action_dispatch/vendor/rack-1.0/rack/reloader.rb @@ -0,0 +1,64 @@ +require 'thread' + +module Rack + # 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. + + class Reloader + def initialize(app, secs=10) + @app = app + @secs = secs # reload every @secs seconds max + @last = Time.now + end + + def call(env) + if Time.now > @last + @secs + Thread.exclusive { + reload!(env['rack.errors']) + @last = Time.now + } + end + + @app.call(env) + end + + def reload!(stderr=$stderr) + need_reload = $LOADED_FEATURES.find_all { |loaded| + begin + if loaded =~ /\A[.\/]/ # absolute filename or 1.9 + abs = loaded + else + abs = $LOAD_PATH.map { |path| ::File.join(path, loaded) }. + find { |file| ::File.exist? file } + end + + if abs + ::File.mtime(abs) > @last - @secs rescue false + else + false + end + end + } + + need_reload.each { |l| + $LOADED_FEATURES.delete l + } + + need_reload.each { |to_load| + begin + if require to_load + stderr.puts "#{self.class}: reloaded `#{to_load}'" + end + rescue LoadError, SyntaxError => e + raise e # Possibly ShowExceptions + end + } + + stderr.flush + need_reload + end + end +end |