aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-20 13:26:42 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-20 13:26:42 +0200
commit71703c98ba2bf65a6fed94917b039827cb63bace (patch)
tree186380d706185edae8b801f48354cec6ab0dd08e /activesupport/lib
parent9e081caee74e6d08035a8835899dcc566536a871 (diff)
downloadrails-71703c98ba2bf65a6fed94917b039827cb63bace.tar.gz
rails-71703c98ba2bf65a6fed94917b039827cb63bace.tar.bz2
rails-71703c98ba2bf65a6fed94917b039827cb63bace.zip
Add ActiveSupport::FileUpdateChecker.
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support.rb4
-rw-r--r--activesupport/lib/active_support/file_update_checker.rb37
2 files changed, 40 insertions, 1 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb
index f93b351655..3ce5476bbd 100644
--- a/activesupport/lib/active_support.rb
+++ b/activesupport/lib/active_support.rb
@@ -39,6 +39,9 @@ require "active_support/dependencies/autoload"
module ActiveSupport
extend ActiveSupport::Autoload
+ autoload :DescendantsTracker
+ autoload :FileUpdateChecker
+
# TODO: Narrow this list down
eager_autoload do
autoload :BacktraceCleaner
@@ -51,7 +54,6 @@ module ActiveSupport
autoload :Concern
autoload :Configurable
autoload :Deprecation
- autoload :DescendantsTracker
autoload :Gzip
autoload :Inflector
autoload :JSON
diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb
new file mode 100644
index 0000000000..c0b5ca4deb
--- /dev/null
+++ b/activesupport/lib/active_support/file_update_checker.rb
@@ -0,0 +1,37 @@
+module ActiveSupport
+ # This class is responsible to track files and invoke the given block
+ # whenever one of these files are changed. For example, this class
+ # is used by Rails to reload routes whenever they are changed upon
+ # a new request.
+ #
+ # routes_reloader = ActiveSupport::FileUpdateChecker.new(paths) do
+ # paths.each { |p| load(p) }
+ # Rails::Application.routes.reload!
+ # end
+ #
+ # ActionDispatch::Callbacks.to_prepare do
+ # routes_reloader.execute_if_updated
+ # end
+ #
+ class FileUpdateChecker
+ attr_reader :paths, :last_update_at
+
+ def initialize(paths, calculate=false, &block)
+ @paths = paths
+ @block = block
+ @last_update_at = updated_at if calculate
+ end
+
+ def updated_at
+ paths.map { |path| File.stat(path).mtime }.max
+ end
+
+ def execute_if_updated
+ current_update_at = self.updated_at
+ if @last_update_at != current_update_at
+ @last_update_at = current_update_at
+ @block.call
+ end
+ end
+ end
+end \ No newline at end of file