Can I get down now dad?

Tramps like us, baby we were born to code

Theatre of dreams!



Theatre of dreams!, originally uploaded by Spuggy.

- Taken at 11:53 AM on March 29, 2008 - cameraphone upload by ShoZu

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.

Glastonbury 2008 - The Field of Avalon



DSC00019, originally uploaded by Spuggy.

Festival Ukes will be at the Glastonbury Festival again this year. We have moved and now will be in the Field of Avalon.

Lets hope it doesn’t ming down with rain again.

;-)

Grails Object Relational Mapping

“Stone me! that’s a boring blog title, can’t we have more stuff about Ukuleles or pictures of pints of beer” I hear you cry.

Well no.

Grails has a nice way of mapping domain objects together, see these objects for a pretend GTD app:

class Project {

String description
String type
Integer priority
Date dueDate

static hasMany = [ nextActions:NextAction]

static constraints = {
description(nullable:false,length:1..50)
type(inList:[”Home”,”Work”])
type(nullable:false)
}

}
class NextAction {

String description
String context
String notes
Integer priority

static constraints = {
description(blank:false,length:5..30)
context(nullable:false,blank:false,inList:[”email”,”errand”])
}

static belongsTo = Project

}

Hopefully you can easily see the magic here. The hasMany and belongsTo settings join these two objects together in one to many relationship. Better still when you run the Grails app it auto generates the database schema for these objects and an intermediate table to join em - possibly a bit too normalized for me but still.

Even better, if I choose to add a new property to one of the classes the application relaunches after I make the save and the new column is added to the database schema.

Oooo its like magic.

Sure beats the make change, add some xml, recompile, look out of window and restart context hell of normal Java development. 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.

Ruby on Rants

Bloke behind the Mongrel servery thing for Ruby goes nutty rant loontastic.

I have a big soft spot for people who swear a lot.

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.

links for 2008-03-15

I aim to kill you Keith, or see you hang.

cruelsea.jpgAfter finally cracking during high school musical and bursting into tears at the god awfulness of it all I decided it was high time the family watched films I like. I suddenly had the urge to have at my finger tips those films that are on all the time but you never watch. Stiff upper lip British war films and a jolly good western.

Here is my selection:

Butch Cassidy and the Sundance kid
True Grit
Battle of the River Plate
The Cruel Sea
In which we serve
They dive at dawn.

Cheap as chips of course. They arrive on the weekend, and in anticipation I have been shouting “Rooster Cogburn” quotes at people. My favorite is:

“I mean to kill you in one minute, [insert name]. Or see you hanged in Fort Smith at Judge Parker’s convenience. Which’ll it be?”

Its then probably a good idea to put your mouse cable between your teeth and let off imaginary shots. Try it yourself at the water cooler at work, if nothing else it’ll kill time.

Rails or Grails

I have just emerged - blinking - from a long splurge of Java server side development. Bluetrail’s products are based on the Spring/Hibernate stack and while I can appreciate the power of these technologies they certainly aren’t the most fleet of foot. Productivity is not as high as I would like and to be honest web programming with Java can be pretty turgid, what with all the build scripts, XML, compilations and Tomcat context reloads.

As ever, I am always casting my eye over the fence and very attracted to the emerging “code by convention” frameworks. The two contender of course are “Ruby on Rails” and the more Java (or should I say Groovy) based “Groovy on Grails“.

The web is full of talk about these two technologies but I was trying to distill in my mind what the key strengths and weaknesses are:

Ruby on Rails:

Ruby looks brilliant, it is all in one box as opposed to lots of different libraries and the community is strong. The language is significantly different to Java but should be easy enough to pick up. Development productivity is pushed even further by the hot fix nature of the tools - more quick changes as opposed to context restarts and re-compilations.

On the downside, the deployment looks a bit messy - my hoster is already releasing a new environment based on Mongrel having only just started doing Ruby hosting last year. As I have said most of our stuff is written in Java so moving to Ruby would mean total re-writes - sigh. For this reason I am also no not convinced that Ruby will be taken up in the corporate environment in any big way. This is important as it is nice to have skills you can sell in to the blokes with cash now and again.

Finally I don’t buy all the scalability bollocks people keep on about. Twitter may have had problems but then it is a mentally busy site. For 96% of us (statistics provided my dog Betty) this doesn’t matter a jot - productivity is the big payback.

Grails

What makes Grails really attractive is that is promises all the productivity benefits of ROR but is built on the Java/Hibernate stack so you can still use some of you existing Java skills and potentially integrate into you existing Java code and libraries. You will have to learn Groovy but then you’ll have to learn Ruby too so that is canceled out.

The product of a Grails app is of course a WAR file so this should deploy to your existing Java server environment in the same way - no farting about with new hosting environments.

The downsides appear to be that Grails is not as mature as ROR. I think it only got it’s 1.0 release last year and I am never an early adopter of anything if I can help it. That said, it certainly appears to have got some traction out there so it will have a future. Another thing that turns me off is that it is not as quick as rails in making little changes. This is one of the big things that breaks my heart about Java server side development …. sigh.

Next Steps
My head tells me to invest a bit of time in Grails. I love the look of ROR but I can’t be arsed to bin all of my existing code. The next step then is to buy a Groovy on Grails book and leave it in the upstairs toilet.

;-)

« Previous Entries