class ActiveSupport::EventedFileUpdateChecker
:nodoc: all
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 + @dirs.keys).map { |f| @ph.existing_parent(f) } dtw.compact! dtw.uniq! @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)
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) 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 Listen.to(*dtw, &method(:changed)).start end end
def updated?
def updated? @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| if @dirs.fetch(dir, []).include?(ext) break true elsif dir == @lcsp || dir.root? break false end end end end