class Guard::Watcher
processing the file system change result.
When a watcher matches a change, an optional action block is executed to enable
The watcher defines a RegExp that will be matched against file system modifications.
def self.match_files(guard, files)
-
(Array- the matched watcher response
Parameters:
-
files(Array) -- the changed files -
guard(Guard::Guard) -- the guard which watchers are used
def self.match_files(guard, files) guard.watchers.inject([]) do |paths, watcher| files.each do |file| if matches = watcher.match(file) if watcher.action result = watcher.call_action(matches) if guard.options[:any_return] paths << result elsif result.respond_to?(:empty?) && !result.empty? paths << Array(result) end else paths << matches[0] end end end guard.options[:any_return] ? paths : paths.flatten.map { |p| p.to_s } end end
def self.match_files?(guards, files)
-
(Boolean)- Whether a file matches
Parameters:
-
files(Array) -- the files to test -
guards(Array) -- the guards to use the watchers from
def self.match_files?(guards, files) guards.any? do |guard| guard.watchers.any? do |watcher| files.any? { |file| watcher.match(file) } end end end
def self.match_guardfile?(files)
-
(Boolean)- whether one of these files is the Guardfile
Parameters:
-
the(Array) -- files to test
def self.match_guardfile?(files) files.any? { |file| "#{ Dir.pwd }/#{ file }" == Dsl.guardfile_path } end
def call_action(matches)
-
(String)- the final paths
Parameters:
-
matches(String, MatchData) -- the matched path or the match from the Regex
def call_action(matches) begin @action.arity > 0 ? @action.call(matches) : @action.call rescue Exception => e UI.error "Problem with watch action!\n#{ e.message }\n\n#{ e.backtrace.join("\n") }" end end
def initialize(pattern, action = nil)
-
action(Block) -- the action to execute before passing the result to the Guard -
pattern(String, Regexp) -- the pattern to be watched by the guard
def initialize(pattern, action = nil) @pattern, @action = pattern, action @@warning_printed ||= false # deprecation warning if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/ unless @@warning_printed UI.info "*"*20 + "\nDEPRECATION WARNING!\n" + "*"*20 UI.info <<-MSG You have a string in your Guardfile watch patterns that seem to represent a Regexp. Guard matches String with == and Regexp with Regexp#match. You should either use plain String (without Regexp special characters) or real Regexp. MSG @@warning_printed = true end UI.info "\"#{@pattern}\" has been converted to #{ Regexp.new(@pattern).inspect }\n" @pattern = Regexp.new(@pattern) end end
def match(file)
-
(Array- an array of matches (or containing a single path if the pattern is a string))
Parameters:
-
file(String) -- the file to test
def match(file) f = file deleted = file.start_with?('!') f = deleted ? f[1..-1] : f if @pattern.is_a?(Regexp) if m = f.match(@pattern) m = m.to_a m[0] = file m end else f == @pattern ? [file] : nil end end
def match_file?(file)
- Use .match instead
def match_file?(file) UI.info "Guard::Watcher.match_file? is deprecated, please use Guard::Watcher.match instead." match(file) end