文件更新检查器
FileUpdateChecker
指定了 Rails 用于监视文件和控制重新加载的 API。该 API 依赖于四种方法
-
initialize
,它期望两个参数和一个块,如下所述。 -
updated?
,如果文件系统中有更新,则返回布尔值。 -
execute
,在初始化时执行给定的块,并更新最新的监视文件和时间戳。 -
execute_if_updated
,仅在更新时执行块。
初始化后,只有在文件系统中确实发生更改时,调用 execute_if_updated
才会执行块。
Rails 使用此类在每次新请求时重新加载 I18n 框架,无论何时它们发生更改。
i18n_reloader = ActiveSupport::FileUpdateChecker.new(paths) do
I18n.reload!
end
ActiveSupport::Reloader.to_prepare do
i18n_reloader.execute_if_updated
end
- E
- N
- U
类公共方法
new(files, dirs = {}, &block) 链接
它在初始化时接受两个参数。第一个是文件数组,第二个是可选的目录哈希。哈希必须以目录作为键,值是该目录下要监视的扩展名数组。
此方法还必须接收一个块,该块将在路径更改时调用。在 FileUpdateChecker
初始化后,无法更改文件数组和目录列表。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/file_update_checker.rb, line 44 def initialize(files, dirs = {}, &block) unless block raise ArgumentError, "A block is required to initialize a FileUpdateChecker" end @files = files.freeze @glob = compile_glob(dirs) @block = block @watched = nil @updated_at = nil @last_watched = watched @last_update_at = updated_at(@last_watched) end
实例公共方法
execute() 链接
执行给定的块,并更新最新的监视文件和时间戳。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/file_update_checker.rb, line 82 def execute @last_watched = watched @last_update_at = updated_at(@last_watched) @block.call ensure @watched = nil @updated_at = nil end
execute_if_updated() 链接
如果更新,则执行给定的代码块。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/file_update_checker.rb, line 92 def execute_if_updated if updated? yield if block_given? execute true else false end end
updated?() 链接
检查是否有任何条目被更新。如果是,则在通过execute
或execute_if_updated
执行代码块之前,将监视和/或更新时间值缓存起来。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/file_update_checker.rb, line 63 def updated? current_watched = watched if @last_watched.size != current_watched.size @watched = current_watched true else current_updated_at = updated_at(current_watched) if @last_update_at < current_updated_at @watched = current_watched @updated_at = current_updated_at true else false end end end