class ActiveSupport::EventedFileUpdateChecker

:nodoc: all
# => “changed”
checker.execute_if_updated
# => true
checker.updated?
FileUtils.touch(“/tmp/foo”)
# => nil
checker.execute_if_updated
# => false
checker.updated?
checker = ActiveSupport::EventedFileUpdateChecker.new() { 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 boot!

def boot!
  normalize_dirs!
  unless @dtw.empty?
    Listen.to(*@dtw, &method(:changed)).start
  end
end

def changed(modified, added, removed)

def changed(modified, added, removed)
  unless updated?
    @updated.make_true if (modified + added + removed).any? { |f| watching?(f) }
  end
end

def directories_to_watch

def directories_to_watch
  dtw = @files.map(&:dirname) + @dirs.keys
  dtw.compact!
  dtw.uniq!
  normalized_gem_paths = Gem.path.map { |path| File.join path, "" }
  dtw = dtw.reject do |path|
    normalized_gem_paths.any? { |gem_path| path.to_s.start_with?(gem_path) }
  end
  @ph.filter_out_descendants(dtw)
end

def execute

def execute
  @updated.make_false
  @block.call
end

def execute_if_updated

def execute_if_updated
  if updated?
    yield if block_given?
    execute
    true
  end
end

def initialize(files, dirs = {}, &block)

:nodoc: all

# => "changed"
checker.execute_if_updated
# => true
checker.updated?

FileUtils.touch("/tmp/foo")

# => nil
checker.execute_if_updated
# => false
checker.updated?
checker = ActiveSupport::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)
  unless block
    raise ArgumentError, "A block is required to initialize an EventedFileUpdateChecker"
  end
  @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
  dtw = directories_to_watch
  @dtw, @missing = dtw.partition(&:exist?)
  if @dtw.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
      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
  boot!
end

def normalize_dirs!

def normalize_dirs!
  @dirs.transform_keys! do |dir|
    dir.exist? ? dir.realpath : dir
  end
end

def shutdown!

def shutdown!
  Listen.stop
end

def updated?

def updated?
  @boot_mutex.synchronize do
    if @pid != Process.pid
      boot!
      @pid = Process.pid
      @updated.make_true
    end
  end
  if @missing.any?(&:exist?)
    @boot_mutex.synchronize do
      appeared, @missing = @missing.partition(&:exist?)
      shutdown!
      @dtw += appeared
      boot!
      @updated.make_true
    end
  end
  @updated.true?
end

def watching?(file)

def watching?(file)
  file = @ph.xpath(file)
  if @files.member?(file)
    true
  elsif file.directory?
    false
  else
    ext = @ph.normalize_extension(file.extname)
    file.dirname.ascend do |dir|
      matching = @dirs[dir]
      if matching && (matching.empty? || matching.include?(ext))
        break true
      elsif dir == @lcsp || dir.root?
        break false
      end
    end
  end
end