Archive for the 'grails' Category

Dec 1st 2008 Groovy Makes Iteration Easy

Out of the box, groovy gives you a number of powerful methods to iterate over lists and maps:

def fibList = [1, 1, 2, 3, 5]
 
fibList.each { println it }  // prints all of the numbers in the list
assert fibList.any { it == 3 }
assert fibList.every { it > 0 }
assert fibList.collect { it - 1 } == [0, 0, 1, 2, 4]
assert fibList.findAll { it > 1 && it < 5 } == [2, 3]
assert fibList.find { it > 1 } == 2
assert fibList.inject("fib: ") { str, val -> str << val }.toString() == "fib: 11235"

That’s really nice if you’re working with raw lists and maps, but what if you have a class that doesn’t extend list or map? How hard is it to empower that class with the groovy iteration methods? If this were Java, you’d likely need to implement an interface with these methods (and throw a “not implemented” exception for those you didn’t feel like taking the time to implement).

Since it’s not Java, but groovy (and you’ve read the title of this blog post :), you know it’s easy!
Continue Reading »

7 Comments » Posted by tednaleid / grails and groovy and shortcut

Jun 3rd 2008 Distributed Source Control with Mercurial Presentation

I gave a presentation at work today on Distributed Version Control Systems and Mercurial. We’re currently using Subversion, and I’m nudging my co-workers into getting interested in the benefits of distributed version control over a centralized system.

The presentation starts with the pros and cons of distributed systems and gives a brief overview of the top 3 most popular DVCS systems:

I go into the details of why I chose to use Mercurial and describe some common usage patterns for people used to using Subversion.

I then did a live-coding session where I created a quick grails application, added it to a new repository, cloned the repository and pushed changes back and forth to show how mercurial handles merging and history.

I also suggest how developers can use mercurial as a “super client” to enable much of this power while still working on a team that uses subversion.
Continue Reading »

5 Comments » Posted by tednaleid / command line and grails and mercurial and osx

May 27th 2008 Syntactic Sugar in Groovy and Ruby

Over on the RailsEnvy blog (the guys who made the “Rails vs. X commercials” last year), there’s an article that highlights some of the nice syntactic sugar that Groovy has that’s currently not available in Ruby.

I actually came to Groovy/Grails after working with Ruby/Rails for about a year. Both languages and framworks have their strengths and it’s nice to see cross-pollination of good ideas going both ways. From the comments, it sounds like Ruby 1.9 is getting some of the features that we’ve had in Groovy for a while.
Continue Reading »

4 Comments » Posted by tednaleid / grails and groovy

May 19th 2008 Groovy: Don’t Fear the RegExp

Some people, when confronted with a problem, think “I know, I’ll use regular expressions!”
Now they have two problems. — Jaime Zawinski

There is a common and well-earned aversion in the Java world to regular expressions. Prior to Java 1.4, regular expressions weren’t even part of the core language. Post 1.4, using regular expressions is still a painful task of working with Pattern and Matcher objects. Lots of typing is involved to make anything happen. It’s difficult enough that most Java devs don’t end up using them enough to actually remember how to read a regular expression, and they need to dig up the JavaDocs (or cut and paste an old example), every time they want to use them.

This aversion has persisted into the Groovy community to a level that I haven’t seen in other dynamic scripting languages like Ruby, Python, and (obviously) Perl.

The current regexp docs that pop up when doing a google search are all outdated and don’t use any of the best techniques that are available in the groovy 1.5.X and 1.6-beta code that is now available. The recent Groovy Recipes book doesn’t have an entry for regular expressions in the index, and I was unable to find a single example of a regular expression in the entire book.

This is unfortunate because Groovy makes using regular expressions much easier than in Java. Under the covers, you’re still working with the same old Java Pattern and Matcher objects, but the Groovy syntax and additions to those classes are pleasant to work with.
Continue Reading »

10 Comments » Posted by tednaleid / grails and groovy and unit testing

May 7th 2008 What methods does my Groovy/Grails class have?

If you’ve ever wondered what methods a groovy class has available for you to call, all you need to do is ask the metaClass:

"foo".metaClass.methods*.name

Result:

["equals", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait", "wait", 
"wait", "charAt", "codePointAt", "codePointBefore", "codePointCount", "compareTo", 
"compareTo", "compareToIgnoreCase", "concat", "contains", "contentEquals", 
"contentEquals", "copyValueOf", "copyValueOf", "endsWith", "equals", 
"equalsIgnoreCase", "format", "format", "getBytes", "getBytes",
 "getBytes", "getChars", "hashCode", "indexOf", "indexOf", "indexOf", 
"indexOf", "intern", "lastIndexOf", "lastIndexOf", "lastIndexOf", "lastIndexOf", 
"length", "matches", "offsetByCodePoints", "regionMatches", "regionMatches", 
"replace", "replace", "replaceAll", "replaceFirst", "split", "split", "startsWith",
 "startsWith", "subSequence", "substring", "substring", "toCharArray", 
"toLowerCase", "toLowerCase", "toString", "toUpperCase", "toUpperCase", 
"trim", "valueOf", "valueOf", "valueOf", "valueOf", "valueOf", "valueOf", 
"valueOf", "valueOf", "valueOf"]

There’s a lot of duplication in there, and the order is random, so I’ll often fix that like this:

"foo".metaClass.methods*.name.sort().unique()

Result:

["charAt", "codePointAt", "codePointBefore", "codePointCount", "compareTo",
 "compareToIgnoreCase", "concat", "contains", "contentEquals", "copyValueOf", 
"endsWith", "equals", "equalsIgnoreCase", "format", "getBytes", "getChars",
 "getClass", "hashCode", "indexOf", "intern", "lastIndexOf", "length", "matches", 
"notify", "notifyAll", "offsetByCodePoints", "regionMatches", "replace", "replaceAll",
 "replaceFirst", "split", "startsWith", "subSequence", "substring", "toCharArray", 
"toLowerCase", "toString", "toUpperCase", "trim", "valueOf", "wait"]

Continue Reading »

4 Comments » Posted by tednaleid / grails and groovy and shortcut

May 1st 2008 Using Mercurial as a “super client” for Subversion

There’s a blog post by one of the subversion developers talking about the future of subversion in a DVCS world.

I agree with parts of the post (and many of the comments) that subversion will continue to be around for a long time, but that more and more developers will start using tools like mercurial and git as a “super client” for a normal centralized version control repository.

Continue Reading »

3 Comments » Posted by tednaleid / command line and grails and osx and shortcut

Apr 23rd 2008 Converting wordpress blog from wordpress.com to slicehost slice

I just finished the conversion over from a wordpress.com hosted blog to a wordpress blog that I’m running on a slicehost slice.

I think that it’s good to maintain your own linux server and get your hands dirty from time in Apache rewrite rules and php code.

The biggest reason for the switch is that the wordpress instance on wordpress.com completely mangles pasted grails and groovy code. It tries to make things pretty by changing a double dash to an mdash, single dashes to an ndash and double quotes to “smart” quotes (an oxymoron if there ever was one). Not to mention the manual &gt; and &lt; that you need to enter instead of > and <. These insidious changes make pasted code worthless and painful for readers to use.

After struggling with trying to get the formatting working correctly for a few weeks, I decided to host my own blog and use the great wp-syntax plugin.

I did need to do some reformatting on my old posts (as I don’t need to encode things anymore) and I believe that I have everything fixed now.  Let me know if you run across anything I’ve missed in the conversion.

No Comments » Posted by tednaleid / command line and grails and groovy

Apr 3rd 2008 Monitoring HTTP traffic to debug your Grails application

I’m often surprised when I run across developers who don’t have a variety of HTTP traffic monitoring apps as an integral part of their toolkit. When doing Grails development (or any web development really), being able to see the actual information that is going over the wire is invaluable.

Firebug

These days, more and more people are familiar with the amazing Firefox plugin Firebug. It allows you to view request and response information along with many other tricks.

Continue Reading »

7 Comments » Posted by tednaleid / command line and grails and osx

Mar 31st 2008 Using Gant to execute a groovy script within the Grails context (updated)

In a previous post I showed a script that I had created to allow the execution of a groovy script within a grails application context (including access to domain objects, controllers, services, etc). A couple of people reported a problem with the script where they were getting lazy initialization exceptions. I finally tracked this issue down to one where many-to-many relationships are being used between two domain classes.

Here is an updated script that fixes this issue by setting up the hibernate session in the Gant script.
Continue Reading »

13 Comments » Posted by tednaleid / command line and grails and groovy

Mar 25th 2008 Autocomplete Grails Script Names in bash/zsh

Jesse over at Refactr posted a nice tip about using tab completion for the ssh command. It grabs host names and IPs out of the ssh known_hosts file.

That got me to thinking that it would be pretty useful to have tab completion of Grails commands available. At my company, we’ve written about 20 custom Gant scripts and it can sometimes be a problem to remember their names. Running “grails help” often takes too long, so I probably ls scripts once a day or so to remind myself if it’s clean-db or clear-db.

I did a quick google search and found that Scott Davis posted some instructions about a year ago (originally by Doyle@DoyleCentral).

It was a good start, but there were two issues I had with the solution. The first is that it only worked in bash (I prefer zsh) and the second is that it used a static list of script names stored in the GRAILS_HOME directory. Any new scripts, or app specific scripts would need to be manually added to the static list.

Grails Gant scripts can exist in 4 possible locations:

  • $GRAILS_HOME/scripts
  • $USER_HOME/.grails/scripts
  • $PROJECT_HOME/scripts
  • $PROJECT_HOME/plugins/*/scripts

After some playing around, I was able to come up with a bash script that allows for real-time completion of all four potential script repositories. $PROJECT_HOME is considered to be the current directory, so if you’re not in a grails app, you’ll only see completion scripts for the first two.

Continue Reading »

6 Comments » Posted by tednaleid / command line and grails and osx

Next »