1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/file_update_checker.rb

37 lines
981 B
Ruby
Raw Normal View History

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
# 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
#
# i18n_reloader = ActiveSupport::FileUpdateChecker.new(paths) do
# I18n.reload!
2010-06-20 07:26:42 -04:00
# end
#
# ActionDispatch::Callbacks.to_prepare do
# 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
@last_update_at = calculate ? updated_at : nil
2010-06-20 07:26:42 -04:00
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