Search code examples
arduinoguard

How do a I write a custom notifier for guard?


I'm trying to write a custom notifier for guard that turns on an LED with arduino.

Anyway, I've got it working by adding a file to lib/guard/notifiers, and adding my new custom class to Guard::Notifier::NOTIFIERS in lib/guard/notifier.rb.

However, I don't think my new notifier should be a part of the guard gem, so I'd like to create a gem. I couldn't find any examples of custom notifier gems, and I've had a hard time figuring out how to add my custom notifier to guard.

Here is my initial attempt, which adds my custom class to Guard::Notifier::NOTIFIERS with the << operator. It doesn't work, as guard gets reloaded for rspec (I'm guessing in a separate process). I can provide more information on my debugging of that if requested.

My question is: what would the best way to extend guard with a custom notifier as a separate gem be?

And the followup: Is this a feature that could be added to guard?

I would normally have added a github issue with the question (hey, can I help make this better?), but the CONTRIBUTING document says to ask here.


Solution

  • It's true that the Guard core team doesn't want to have questions about the usage of Guard in the issue tracker, simply because it's very time consuming for us and it's often the case that people are just to lazy to read the excellent README. Asking on StackOverflow makes it more likely that users helps themselves, freeing time to develop Guard instead of providing free support. For development related question we guide people to our Google group, where all core members are reading the messages and you probably get a competent answer within a few hours.

    I would not mind to merge a pull request for an exotic notifier, because they come with almost no cost. It's also very likely that we'll extract that module into its own gem in the future, so other people can reuse the notifier functionality, like we did for Listen.

    To finally address your question: You are right, the guard-rspec notifier runs in a subprocess and this is why we have this ugly hack to share the notification settings and your notifier does not work. The reason why it doesn't work is that Guard is loaded without plugins, because it only requires files supplied with the -r option. This means you have only the following possibilities to make your notifier work with rspec:

    • Load your notifier in the cli option with -r, but you need to pass the absolute path to your file, probably with a helper method (simple).
    • Extend the Guard::Notifier to search for notifier classes within any Guard plugin (costly).
    • Monkey patch the rspec_arguments method to include your notifier (not recommended).