檔案更新檢查器
FileUpdateChecker
指定 Rails 用於監控檔案和控制重新載入的 API。API 取決於四個方法
-
initialize
,它預期兩個參數和一個區塊,如下所述。 -
updated?
,如果檔案系統中有更新,則傳回布林值,否則傳回 false。 -
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?() 連結
檢查是否有任何條目已更新。如果有的話,則會快取監控和/或 updated_at 值,直到透過 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