Custom matchers in watches
A watch
statement filters files that changed to match the given regexp.
Since Guard 2.14.x, you can pass a custom matcher if it has the right methods.
This allows you to create powerful rules without complex and unreadable hacks.
Lets say you want to respond only to changes in uncommitted files.
You might have:
only_uncommitted_lib_files = MyGitModifiedMatcher.new
watch(only_uncommitted_lib_files) { |m| "spec/#{m[:path_without_ext]}_spec.rb" }
A pseudo-implementation of the MySpecialMatcher
class would be:
class MyGitModifiedMatcher
class MyMatchData
def initialize(result)
@result = result
end
def to_a
@result.values
end
def [](name)
@result[name]
end
end
def match(filename_or_pathname)
return nil unless `git status -uno`.lines.map(&:chomp).include?(filename_or_pathname)
MyMatchData.new(path_without_ext: File.basename(file,File.extname(file)))
end
end
Feel free to open an issue to discuss improvements.
This wiki and the Guard README document contain a lot of information, please take your time and read these instructions carefully.
If you run into any trouble, you may start by understanding how Guard works.
We provide detailed changes for each Guard release.
Be sure to read the CONTRIBUTING guidelines before reporting a new Guard issue or open a pull request.
If you have any questions about the Guard usage or want to share some information with the Guard community, please go to one of the following places:
- Google+ community
- Google group
- StackOverflow
- IRC channel
#guard
(irc.freenode.net) for chatting