Clean up FileUpdateChecker API.

This commit is contained in:
José Valim 2011-12-13 10:07:02 +01:00
parent 693d2be827
commit 1f5b9bbdb3
5 changed files with 25 additions and 20 deletions

View File

@ -16,8 +16,6 @@ module ActiveSupport
# end # end
# #
class FileUpdateChecker class FileUpdateChecker
attr_reader :paths, :last_update_at
# It accepts two parameters on initialization. The first is # It accepts two parameters on initialization. The first is
# the *paths* and the second is *calculate*, a boolean. # the *paths* and the second is *calculate*, a boolean.
# #
@ -29,12 +27,13 @@ module ActiveSupport
# on initialization, therefore, the first call to execute_if_updated # on initialization, therefore, the first call to execute_if_updated
# will only evaluate the block if something really changed. # will only evaluate the block if something really changed.
# #
# This method must also receive a block that will be the block called # This method must also receive a block that will be called once a file changes.
# once a file changes.
# #
# This particular implementation checks for added files and updated files, # This particular implementation checks for added files and updated files,
# but not removed files. Directories lookup are compiled to a glob for # but not removed files. Directories lookup are compiled to a glob for
# performance. # performance. Therefore, while someone can add new files to paths after
# initialization, adding new directories is not allowed. Notice that,
# depending on the implementation, not even new files may be added.
def initialize(paths, calculate=false, &block) def initialize(paths, calculate=false, &block)
@paths = paths @paths = paths
@glob = compile_glob(@paths.extract_options!) @glob = compile_glob(@paths.extract_options!)
@ -44,7 +43,7 @@ module ActiveSupport
end end
# Check if any of the entries were updated. If so, the updated_at # Check if any of the entries were updated. If so, the updated_at
# value is cached until flush! is called. # value is cached until the block is executed via +execute+ or +execute_if_updated+
def updated? def updated?
current_updated_at = updated_at current_updated_at = updated_at
if @last_update_at != current_updated_at if @last_update_at != current_updated_at
@ -55,8 +54,11 @@ module ActiveSupport
end end
end end
# Flush the cache so updated? is calculated again # Executes the given block expiring any internal cache.
def flush! def execute
@last_update_at = updated_at
@block.call
ensure
@updated_at = nil @updated_at = nil
end end
@ -64,14 +66,11 @@ module ActiveSupport
# always flush the cache. # always flush the cache.
def execute_if_updated def execute_if_updated
if updated? if updated?
@last_update_at = updated_at execute
@block.call
true true
else else
false false
end end
ensure
flush!
end end
private private
@ -86,7 +85,9 @@ module ActiveSupport
end end
def compile_glob(hash) #:nodoc: def compile_glob(hash) #:nodoc:
hash.freeze # Freeze so changes aren't accidently pushed
return if hash.empty? return if hash.empty?
globs = [] globs = []
hash.each do |key, value| hash.each do |key, value|
globs << "#{key}/**/*#{compile_ext(value)}" globs << "#{key}/**/*#{compile_ext(value)}"

View File

@ -10,7 +10,11 @@ module I18n
config.i18n.fallbacks = ActiveSupport::OrderedOptions.new config.i18n.fallbacks = ActiveSupport::OrderedOptions.new
def self.reloader def self.reloader
@reloader ||= ActiveSupport::FileUpdateChecker.new([]){ I18n.reload! } @reloader ||= ActiveSupport::FileUpdateChecker.new(reloader_paths){ I18n.reload! }
end
def self.reloader_paths
@reloader_paths ||= []
end end
# Add <tt>I18n::Railtie.reloader</tt> to ActionDispatch callbacks. Since, at this # Add <tt>I18n::Railtie.reloader</tt> to ActionDispatch callbacks. Since, at this
@ -59,7 +63,7 @@ module I18n
init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks) init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks)
reloader.paths.concat I18n.load_path reloader_paths.concat I18n.load_path
reloader.execute_if_updated reloader.execute_if_updated
@i18n_inited = true @i18n_inited = true

View File

@ -63,7 +63,7 @@ class FileUpdateCheckerWithEnumerableTest < Test::Unit::TestCase
FileUtils.touch(FILES) FileUtils.touch(FILES)
assert checker.updated? assert checker.updated?
assert checker.execute_if_updated checker.execute
assert !checker.updated? assert !checker.updated?
end end

View File

@ -83,7 +83,7 @@ module Rails
self.reloaders << reloader self.reloaders << reloader
# We need to set a to_prepare callback regardless of the reloader result, i.e. # We need to set a to_prepare callback regardless of the reloader result, i.e.
# models should be reloaded if any of the reloaders (i18n, routes) were updated. # models should be reloaded if any of the reloaders (i18n, routes) were updated.
ActionDispatch::Reloader.to_prepare(:prepend => true, &callback) ActionDispatch::Reloader.to_prepare(:prepend => true){ reloader.execute }
else else
ActionDispatch::Reloader.to_cleanup(&callback) ActionDispatch::Reloader.to_cleanup(&callback)
end end

View File

@ -3,12 +3,12 @@ require "active_support/core_ext/module/delegation"
module Rails module Rails
class Application class Application
class RoutesReloader class RoutesReloader
attr_reader :route_sets attr_reader :route_sets, :paths
delegate :execute_if_updated, :updated?, :to => :@updater
delegate :paths, :execute_if_updated, :updated?, :to => :@updater
def initialize(updater=ActiveSupport::FileUpdateChecker) def initialize(updater=ActiveSupport::FileUpdateChecker)
@updater = updater.new([]) { reload! } @paths = []
@updater = updater.new(paths) { reload! }
@route_sets = [] @route_sets = []
end end