class Guard::Watcher
enable processing the file system change result.
When a watcher matches a change, an optional action block is executed to
modifications.
The watcher defines a RegExp that will be matched against file system
def self.match_files(guard, files)
-
(Array
- the matched watcher response
Parameters:
-
files
(Array
) -- the changed files -
guard
(Guard::Plugin
) -- the Guard plugin which watchers are used
def self.match_files(guard, files) return [] if files.empty? files.inject([]) do |paths, file| guard.watchers.each do |watcher| matches = watcher.match(file) next(paths) unless matches 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) else next(paths) end else paths << matches[0] end break if guard.options[:first_match] end guard.options[:any_return] ? paths : paths.flatten.map(&:to_s) end end
def ==(other)
-
(true, false)
- equal or not
Parameters:
-
other
(Guard::Watcher
) -- other watcher for comparing
def ==(other) action == other.action && pattern == other.pattern end
def call_action(matches)
-
(String)
- the final paths
Parameters:
-
matches
(String, MatchData
) -- the matched path or the match from the
def call_action(matches) @action.arity > 0 ? @action.call(matches) : @action.call rescue => ex UI.error "Problem with watch action!\n#{ex.message}" UI.error ex.backtrace.join("\n") end
def initialize(pattern, action = nil)
-
action
(Block
) -- the action to execute before passing the result to -
pattern
(String, Regexp
) -- the pattern to be watched by the Guard
def initialize(pattern, action = nil) @action = action @pattern = Pattern.create(pattern) end
def match(string_or_pathname)
def match(string_or_pathname) m = pattern.match(string_or_pathname) m.nil? ? nil : Pattern::MatchResult.new(m, string_or_pathname) end