class ActiveSupport::EventedFileUpdateChecker
def initialize(files, dirs = {}, &block)
# => "changed"
checker.execute_if_updated
# => true
checker.updated?
FileUtils.touch("/tmp/foo")
# => nil
checker.execute_if_updated
# => false
checker.updated?
checker = EventedFileUpdateChecker.new(["/tmp/foo"], -> { puts "changed" })
Example:
Note: Forking will cause the first call to `updated?` to return `true`.
is run and there have been changes to the file system.
EventedFileUpdateChecker#execute is run or when EventedFileUpdateChecker#execute_if_updated
and file extensions to watch. It also takes a block that is called when
The file checker takes an array of files to watch or a hash specifying directories
in state.
instead it uses platform specific file system events to trigger a change
The evented file updater does not hit disk when checking for updates
Allows you to "listen" to changes in a file system.
def initialize(files, dirs = {}, &block) @ph = PathHelper.new @files = files.map { |f| @ph.xpath(f) }.to_set @dirs = {} dirs.each do |dir, exts| @dirs[@ph.xpath(dir)] = Array(exts).map { |ext| @ph.normalize_extension(ext) } end @block = block @updated = Concurrent::AtomicBoolean.new(false) @lcsp = @ph.longest_common_subpath(@dirs.keys) @pid = Process.pid @boot_mutex = Mutex.new if (@dtw = directories_to_watch).any? # Loading listen triggers warnings. These are originated by a legit # usage of attr_* macros for private attributes, but adds a lot of noise # to our test suite. Thus, we lazy load it and disable warnings locally. silence_warnings do begin require 'listen' rescue LoadError => e raise LoadError, "Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile", e.backtrace end end end boot! end