Guard running only the first watcher that matches

39 views Asked by At

Is there a way to prevent Guard to run all the watchers that match a file structure but only the first one?

I basically need to instantiate a different object if the file copied in my root folder has a specific structure or not. For instance, if the file name matches \d{2}-\d{6}_\d{5}_\d+_\d+.csv I need to instances object A while for all the other .cvs files object B.

As first attempt I was trying to use negative lookbehind but due to lookbehind limitations it looks like I cannot do that. So, now I'm trying to force Guard to execute only the first watcher that matches.

My Guardfile looks like

guard :my_csv_files do
  watch(%r{^\d{2}-\d{6}_\d{5}_\d+_\d+.csv$})
end

guard :others_csv_files do
   watch(%r{^.+.csv$})
end

Thanks

1

There are 1 answers

0
Cezary Baginski On

This has been available for a while, but was never documented. (Until today).

There's a :first_match option, which does exactly what you want:

https://github.com/guard/guard/wiki/Guardfile-DSL---Configuring-Guard#guard