Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a very basic Guard example?

Tags:

ruby

guard

My goal is to build a simple custom guard with Guard. The gem install and bundler install for my app went fine. My Guardfile contains:

notification :growl

guard 'eyeball' do
  watch %r{^app/(.*)}
  watch %r{^config/(.*)}
  watch %r{^lib/(.*)}
end

Ok, next, I need to tell Guard what to do when a match happens. But I don't know where to do that. (In this case, I want to watch my application for changes and run some arbitrary code. Assume that there isn't a guard available for what I want. I want to learn how to do it myself.)

In true 'blunder and see what errors pop up next' style, when I run guard I get this error message:

ERROR: Could not load 'guard/eyeball' or find class Guard::Eyeball
ERROR: cannot load such file -- guard/eyeball
ERROR: Invalid Guardfile, original error is:
undefined method `new' for nil:NilClass
ERROR: No guards found in Guardfile, please add at least one.
Guard uses Growl to send notifications.
Guard is now watching at '/Users/my-user-name/dev/my-project-name'

So, that gives me a hint that I need to create a guard/eyeball.rb file. Maybe? But how was I supposed to know this from the documentation?

I've read (several times) the very detailed and useful Guard README but haven't found a good simple example that shows someone how to do 'just the basics' of writing your own guard. Unexpectedly, RailsCasts didn't really answer my question either: see RailsCast #264 Guard.

Did I overlook something in the Guard README? Can you help or point to a good example? Thanks!

like image 808
David J. Avatar asked Jun 24 '12 00:06

David J.


People also ask

How do you write a guard statement?

Syntax of guard Statement The syntax of the guard statement is: guard expression else { // statements // control statement: return, break, continue or throw. } Note: We must use return , break , continue or throw to exit from the guard scope.

What is a guard statement?

You use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. Unlike an if statement, a guard statement always has an else clause—the code inside the else clause is executed if the condition is not true.

What makes a good security guard?

Honest and Trustworthy You need to be able to trust your guard with your belongings and business. An honest security guard will make it easier for you to trust them. There will be times when they will have to work alone, and you can't keep an eye on them.


1 Answers

Sweet! I just found a Wiki page on the Guard wiki titled Create a guard that answers my questions. It was not mentioned in the README, so I had to dig for it.

like image 150
David J. Avatar answered Sep 28 '22 02:09

David J.