Archive for the ‘Uncategorized’Category

Groovy, let me count the ways in which I love thee

Oh the wonder and the joy. I’ve been fixing some issues and adding features to the Grails Functional Testing plugin (now informally known as The G-Func) and had cause to marvel at some Groovy treats.

The problem: HtmlUnit, which the plugin uses for HTTP interactions, has an extensive hierarchy of classes representing page and form elements. There is however no way to indentify some of these as specific functional groups eg there are 4 different button types, which have no interface or ancestor that is not also shared by some other non-button classes.

So a little ugly logic is needed. For example when the plugin’s form wrapper uses Groovy dynamic property resolution to get a form field for you, it is now doing something like this:

private getFieldValue(f) {
  // Note this switch is polymorphic, order of cases is IMPORTANT
  switch (f.class) {
    case BUTTON_CLASSES:
    case HtmlFileInput.class:
      return f // return the field object itself, not its value
    case HtmlSelect.class:
      return f.selected
    case HtmlRadioButtonInput.class:
      return f.checked
    case HtmlCheckBoxInput.class:
      return f.checked
    case HtmlTextArea.class:
    case HtmlInput.class:
      return f.value
    default:
      throw new RuntimeException("Don’t know how to get a value from form element of type [${f.class}]")
  }
}

Now, what is special about this is:

  1. Groovy’s switch statement is very cool. It calls isCase(value) on the objects used in the case statements. That’s pretty basic Groovy stuff, but is still news to most Java developers.
  2. The use of BUTTON_CLASSES in the case is very cool. That property is a list of HtmlUnit classes that are “buttons” in conceptual terms. Groovy will call isCase on the list, which will return true if the list CONTAINS the switch value. That saved me 4 case lines and made my code more DRY as this check is also done elsewhere.
  3. The isCase implementation on Class added by the Groovy GDK is polymorphic. So the “case HtmlInput.class” actually catches any descendent of HtmlInput – of which there are several. This is also why the case BUTTON_CLASSES is at the top of the switch, as some of those classes extend HtmlInput and would be incorrectly caught by this HtmlInput clause.

Many people will no doubt say I can simplify the code above further – the “.class” are largely not necessary and I could merge the “return f.checked” cases. I may well do in future, but this is there for clarity at the moment :)

  • Twitter
  • Slashdot
  • Delicious
  • Evernote
  • Share/Bookmark
Tags: ,

16

01 2009

Random rambling update

Christmas crush with work… been this way every Christmas for the last 4-5 years apart from last year. Why?

Getting close to release for the Grails CMS app with my work on it kindly funded by JCatalog AB. Getting improved billing and signup stuff up together for my other client.

Struggled a lot with problems with flows in Grails 1.0.4, really got me down to be honest as it has been such a sinkhole for wasted time. Hopefully I can contrib some fixes or at least isolated test cases to help sort this out.

Work is ongoing on the functional test plugin which is rocking already, but needs a little more polish before release. Also ongoing work on the bean fields taglib for Grails.

So much to do so little time. Have to be with the family and keep up my drum practice!

It looks like I’m booked up again until the end of January. If you are interested in hiring me from Feb 2009 onwards, please get in touch now!

  • Twitter
  • Slashdot
  • Delicious
  • Evernote
  • Share/Bookmark

18

12 2008

US Voting system – broken!

The other day, thanks to Garr Reynolds’ always useful Presentation Zen blog, I watch this video of Seth Godin talking about how things are so often “broken” and how people are complicit in creating the breakage or ignoring it, or not complaining about it.

Now it’s a very amusing and quite thought-provoking talk. I now look at things even more critically than I used to, and annoy my wife by telling her how broken something is.

Anyway, the US voting system is broken. Ignore the problems with ballot papers, dodgy voting machines, and the whole lack of security in the electronic voting machines…

The US voting system is broken, I can tell, because all I hear on the news is how people are voting EARLY and yet it is taking them 3-4 hours in many cases, from entering the building to casting their vote.

This is utterly utterly broken, and you have to wonder if it is broken intentionally (one of Seth Godin’s 7 or so reasons for being broken is deliberately broken by the maker).

After all, a long delay to vote is rather a good way of filtering out certain kinds of voters – those with low paid jobs and unhelpful bosses – those who are carers – those who are young and have something better to do with their four hours. Could voting delays alone be the single nascent reason behind the Republican presence in the White House for so long?

Can you imagine the outcry if it took people 4 hours to vote in the UK!?

For your information, last time I voted in the UK it took 5 minutes.

  • Twitter
  • Slashdot
  • Delicious
  • Evernote
  • Share/Bookmark

03

11 2008

Quote of the day

The State will tell us how to teach and what results to aim for, and what the State prescribes will be bad. Its targets are the worst ones imaginable, yet it expects to get the best possible results. Today’s politics work in the direction of regimentation, and it will go even further than this in its attempts to make people conform. Human beings will be treated like puppets on strings, and this will be treated as progress in the extreme. Institutions like schools will be organised in the most arrogant and unsuitable manner

- Dr Rudolf Sterner, from an address given on 20 August 1919 (emphasis added).

Our battle is continuing to prevent the EYFS from clobbering the good work done by Steiner Waldorf schools in the UK.

  • Twitter
  • Slashdot
  • Delicious
  • Evernote
  • Share/Bookmark

30

09 2008

Grails mail plugin 0.4 released, as well as the first “Grails Rocks” screencast

I released version 0.4 of the grails-mail plugin in the Grails plugin repo the other day, but have not posted about it as I’ve been busy working on-site. I’ve also been putting together a screencast that shows how to use the plugin in the most basic scenario and also render content from GSPs from within services.

To install the plugin, which adds support for rendering the mail body from a GSP from within a controller, service or job, run: grails install-plugin mail

Full docs for the mail plugin are here.

To view the “Using the Grails mail plugin” screencast, click on the thumbnail or go to the fledgling “Grails Rocks” page.

This is the first Grails Rocks screencast, but I hope to do many more. The next one is already planned, for the forthcoming release of the Email Confirmation plugin – which was dependent on this mail plugin 0.4 release.

  • Twitter
  • Slashdot
  • Delicious
  • Evernote
  • Share/Bookmark

25

09 2008