2010-06-20 07:26:42 -04:00
|
|
|
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
|
2010-07-01 11:07:48 -04:00
|
|
|
# is used by Rails to reload the I18n framework whenever they are
|
|
|
|
# changed upon a new request.
|
2010-06-20 07:26:42 -04:00
|
|
|
#
|
2010-07-01 11:07:48 -04:00
|
|
|
# i18n_reloader = ActiveSupport::FileUpdateChecker.new(paths) do
|
|
|
|
# I18n.reload!
|
2010-06-20 07:26:42 -04:00
|
|
|
# end
|
|
|
|
#
|
2010-12-19 18:58:58 -05:00
|
|
|
# ActionDispatch::Reloader.to_prepare do
|
2010-07-01 11:07:48 -04:00
|
|
|
# i18n_reloader.execute_if_updated
|
2010-06-20 07:26:42 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
class FileUpdateChecker
|
|
|
|
attr_reader :paths, :last_update_at
|
|
|
|
|
|
|
|
def initialize(paths, calculate=false, &block)
|
|
|
|
@paths = paths
|
|
|
|
@block = block
|
2010-06-21 04:28:02 -04:00
|
|
|
@last_update_at = calculate ? updated_at : nil
|
2010-06-20 07:26:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at
|
2011-12-12 08:53:25 -05:00
|
|
|
# TODO: Use Enumerable check once we get rid of 1.8.7
|
|
|
|
all = paths.is_a?(Array) ? paths : Dir[paths]
|
|
|
|
all.map { |path| File.mtime(path) }.max
|
2010-06-20 07:26:42 -04:00
|
|
|
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
|
2010-06-21 04:28:02 -04:00
|
|
|
end
|