Ad-Hockery

ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary. Jargon File

Mongo dynamic attributes and Grails unit tests

When using Mongo DB with GORM it’s possible to assign to dynamic attributes of a domain class. However, you’ll find that when you write unit tests for code that uses this feature it isn’t supported. It’s easy to emulate, though.

Read more…

Groovy gotchas: overloading the assignment operator

Note

I reported this issue as GROOVY-6084 and it has been fixed as of Groovy 2.4.0-beta-4. I’m keeping the blog post here for historical interest.

When assigning to a property of an object in Groovy using the assignment operator = Groovy will look for a bean property style setter method. For example x.foo = y is equivalent to x.setFoo(y). Or is it?

Since meta-methods can be overloaded with different parameter types what will be printed to sysout if we run this script? (Remember that Groovy - unlike Java - dispatches to the most specific applicable method signature).

Object.metaClass.with {
    setFoo = {
        println "setFoo(Object)"
    }
    setFoo = { String s ->
        println "setFoo(String)"
    }
}

def o = new Object()
o.setFoo("foo")
o.setFoo(o)
o.foo = "foo"
o.foo = o
Read more…

Groovy gotcha: for loops and closure scope

You probably know that Groovy closures retain information about the scope in which they were created. The closure body can refer to values that were in scope where the closure was declared. There is a gotcha here that has bitten me a few times, though. That’s when closures are created in a for loop.

Consider this example.

def fns = []
for (i in (1..5)) {
    fns << {->
        println i
    }
}
fns.each { it() }

What will get printed to sysout? Give up? You can run the script on Groovy Web Console. The output is this:

5
5
5
5
5
Read more…

Stateful interactions in Spock

The Java mocking library jMock has a nice feature for dealing with verifying mock interactions in stateful circumstances. I first came across it when reading Growing Object-Oriented Software Guided By Tests (GOOS) by Steve Freeman & Nat Pryce.

I was curious as to whether I could implement something similar with Spock. There’s no syntactic support right now (although there is an open issue) but it’s not that complex to achieve something adequate.

Read more…

Grails builds on Travis CI

Travis CI is a cloud based continuous integration service. It’s a great way to automate test runs for projects hosted on GitHub. Since the Grails wrapper was added to Grails in version 2.1 you can use Travis to build Grails apps.

Read more…

Groovy & the public keyword

One of the first things you learn in Groovy is that unlike Java public is the default scope for properties and methods declared on classes. Most developers get into the habit of simply omitting the public keyword everywhere. But, is there any situation where it’s the right thing to use? Actually, yes.

Read more…

Deprecating Grails plugins

I’ve written a number of Grails plugins and the fact is that some of them are effectively unsupported. I’ve only got so much time & I’m juggling work, family, conference speaking, my own projects and open source. Any plugin development I do is going to be driven by the requirements of work or other projects, so even those plugins I still consider as supported might not get updated as often as some people would like.

Read more…

Dynamic templates in Angular routes

We’re building an application where content can be published using different layouts selected by an editor. In Angular this translates to a situation where the same route & controller will need to display content from the same $http endpoint using a range of different templates. The name of the template is included in the JSON data returned by the $http endpoint so it’s not known at the time the route is triggered.

Read more…

CSS background transformations

CSS3 includes a bunch of transform options allowing things like rotation & distortion of element boxes. I wanted to apply that to achieve an effect inspired by mid 20th Century Googie signage.

My goal was to distort just the background behind an element whilst leaving the text alone. The text should also be able to "escape" from the background, for example letters can overflow beyond the background.

Naturally, being a CSS purist I also wanted to do all this without introducing non-semantic extra elements into the document.

Read more…

Ad-Hockery on Tumblr

There’s nothing more boring than an "I moved my blog" post but a couple of people have actually asked me to write up something about why I chose Tumblr & how I used Grunt to help me build the theme. So – sorry – here’s my "I moved my blog" post…

Read more…

Web Statistics