2011-02-06 16:41:56 -05:00
|
|
|
module ActiveSupport
|
|
|
|
class FileWatcher
|
|
|
|
class Backend
|
|
|
|
def initialize(path, watcher)
|
|
|
|
@watcher = watcher
|
|
|
|
@path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger(files)
|
|
|
|
@watcher.trigger(files)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@regex_matchers = {}
|
|
|
|
end
|
|
|
|
|
2011-02-08 13:48:01 -05:00
|
|
|
def watch(pattern, &block)
|
|
|
|
@regex_matchers[pattern] = block
|
2011-02-06 16:41:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def trigger(files)
|
|
|
|
trigger_files = Hash.new { |h,k| h[k] = Hash.new { |h2,k2| h2[k2] = [] } }
|
|
|
|
|
|
|
|
files.each do |file, state|
|
2011-02-08 13:48:01 -05:00
|
|
|
@regex_matchers.each do |pattern, block|
|
|
|
|
trigger_files[block][state] << file if pattern === file
|
2011-02-06 16:41:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
trigger_files.each do |block, payload|
|
|
|
|
block.call payload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|