Can I get down now dad?

Tramps like us, baby we were born to code

Grails Console is … well… groovy dammit

picture-5.png

Doing more Groovy on Grails dev today and have really got in to the Grails Console. What this allows you to do is to fire up a console from within the context of your application and then dick about with code snippets. The context bit is important as you can run all the data access stuff just as you would in you application. In the above example I list the contents of the JadLine table.

The real benefit for me, particularly when on a leaning curve of a new language, is the ability to run a code snippets with out having to arse about in browsers. This really chops the corner off the usual code, reload and grumble process of web development in Java.

To run the Grails console, cd to you project directory and enter “Grails console”

Woof

Groovy on Grails for real

grails.jpg
Needed to knock up a quick app to deliver Jars and Jads and decided it was time to have a go at Groovy on Grails for real.

Although learning curves can be a bit tiresome, overall the experience has been pretty positive. The things I like so far are:

  • Being able to create a domain class and have it create all the relational mapping for me.
  • Being able to add fields to this class and have it update the table.
  • Creating a controller and have automatic CRUD scaffolding in place so I can see if things look ok.
  • Being able to update code and have the context loaded automatically … ace. This is a little flaky sometimes, but that might be my lack of understanding.
  • Having the freedom and no typee-ness of Groovy, no static typing is good most times
  • Knowing that I can use a lot of stuff I know like the String functions, no massive syntax change
  • Knowing it is all Spring, Hibernate and Sitemesh underneath

The other thing that has given me confidence is that SpringSource are now behind the Grails project.

Ooooo it’s all good.

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.