Can I get down now dad?

Tramps like us, baby we were born to code

Groovy and Closures

Groovy and Grails (Ruby too) make a big thing of closures. A closure is defined, slightly unhelpfully, as:

A Groovy closure is like a “code block” or a method pointer. It is a piece of code that is defined and then executed at a later point.

If you are not use to them they can be tricky to grasp but they are hugely powerful and in many cases make code easier to read. See my cheesey code to print out the files in a directory:

groovyexp.png

This is using the Groovy eclipse plugin of course

The only problem with closures is that they are manna are from heaven to the lets-write-the-code-in-least-amount-of-lines-brigade. You know the sort of people, they chain a load of functions and objects together in one line and then stick a regular expression on the end. The technical term for these people, I believe, is bastards.

Groovy and Ruby

Up till now I have used Ruby for the odd scripting job, just to get into the language as it were. One script I use regularly is a one that opens a url a 100 times and sleeps for a few seconds in between. Here is the Ruby code:

require 'net/http'
require 'uri'
response = ''
for i in 0 ... 100
puts(i)
url = URI.parse('http://localhost:8080/frontier/batchProcess.html')
res = Net::HTTP.start(url.host, url.port) {|http|
response=http.get('/frontier/batchProcess.html')
}
puts(response)
sleep(10);
end

as I have gone all Groovy, I decided to re-write this in Groovy. It is very similar but slightly more familiar for us Java herberts:


println "start"
for(i in 1..100) {
url = new URL("http://localhost:8080/frontier/batchProcess.html")
s = url.getText()
println s
println "sleeping ${i}"
sleep(2000)
}
println "stop"

I think that familiarity could be key.

Finally, I had some friends over on the week and they spotted my “Groovy Programming book”. Oh how they laughed and pointed at me. Sigh.

Books, Mac and Light Sabre

dsc00236.JPG

I’ve got the Groovy book, the Grails book, the MacBook and the light sabre …… and yeah a light sabre.

Digging the crazy Grails thing

So here goes, my first Grails app. A simple todo list

First create me an application with: grails create-app todo

Then create a domain class with: grails create-domain-class and tell to create a class called todo.

Edit the domain class, add some fields and constraints as follows:

class Todo {

String description
String type
Date dueDate

static constraints = {
description(unique:true,,blank:false,length:5..30)
type(nullable:false,blank:false,inList:[’work’,'private’])
}
}

Then generate a controller with: grails create-controller and tell it to use Todo.

Finally edit the controller class TodoController.groovy an tell it to dynamically generate the scaffoled code based on the Todo domain class.

class TodoController {

def scaffold = Todo
}

And with just that tiny bit code I have a running app. I can test it locally and when I am happy create a war file with grails war.

Finally, and wondefully I can just upload this to my tomcat server and it works. See the demo app here.

There is a very real chance I may wet my pants.

Final note, the database is just a in memory db so things will disappear.