Groovy magic: asType
Posted by admin - 31/05/10 at 10:05:01 amEvery one who used Grails had to deal (or will have) with XML/JSON converters. They allow to transform object by using syntax like:
import grails.converters.* class TestController { def index = { [a:1, b:2, c:3] as JSON } }
I like this "as" syntax, is very verbose and clear. How we can implement in our code? Simple, just use asType method:
class Foo { def name String toString(){ "Foo name is ${name}" } } class Bar { def name String toString(){ "Bar name is ${name}" } } class Bas { def name String toString(){ "Bas name is ${name}" } Object asType(Class type){ typeWorker[type](this) } @Lazy private def typeWorker = [(Foo):asFoo, (Bar):asBar] private def asFoo = {it -> new Foo(name:it.name) } private def asBar = {it -> new Bar(name:it.name) } } def bas = new Bas(name:"Gringo") println bas println bas as Foo println bas as Bar
and output will be :
Bas name is Gringo
Foo name is Gringo
Bar name is Gringo
Groovy exercises – text wrapping
Posted by admin - 10/02/10 at 12:02:08 amThis is my groovy solution for simple text wrapping problem:
def input = "The quick brown fox jumps over the lazy dog. " String.metaClass.wrap = {token, length -> def line = token def elements = delegate.tokenize().reverse() while(elements.size()){ if ((line + elements.last()).size() < length){ line += ' ' + elements.pop() } else { println line line = token } } if (line != token){ println line } } (input * 10).wrap('>', 72)
Grails and AST transformations – hard start
Posted by admin - 28/01/10 at 05:01:49 pmRecently i tried to use AST transformation in my Burning Image Grails plugin. I want to use them, to mark domain objects that should be extended by some methods and fields that allow to use this object as a container for information about image saved on server.
I follow this "how to" tutorial, but after struggling with code for several hours, its appears (Joshua Burnett was very helpful - thanks) that if you want to use this technique, you should write code as a pure Java classes not Groovy scripts. After that, it run smoothly.
If any one is capable to run AST transformations as groovy scripts (in Grails project), i will be very thankful for "how to".
Mixin is not 100% inheritance
Posted by admin - 28/01/10 at 12:01:08 amGroovy allows us to simulate multiple inheritance by using @Mixin annotation:
class Foo { def sayFoo(){ "hello from foo" } } class Bar { def sayBar(){ "hello from bar" } } @Mixin([Foo, Bar]) class Bas { } def foo = new Foo() def bar = new Bar() def bas = new Bas() assert foo.sayFoo() == bas.sayFoo() assert bar.sayBar() == bas.sayBar()
but unfortunately it will only copy methods and properties, and will not provide mixed type, so when you try something like this:
assert bas instanceof Foo assert bas instanceof Bar
assertion exception will be throw.
Groovy List.contains / GString fuss
Posted by admin - 17/12/09 at 10:12:47 amIf you want to search for String value in the List using contains method, be careful if the argument is GString type.
def foo = 1 assert !['1','2','3'].contains("${foo}") assert ['1','2','3'].contains("${foo}".toString())
Why? Because contains use equals method to match your parameter to each position on the list, and for equals we have:
def foo = "groovy" def bar = "groovy${''}" assert foo.class == java.lang.String assert bar.class == org.codehaus.groovy.runtime.GStringImpl assert foo == bar assert bar == foo assert !foo.equals(bar) assert !bar.equals(foo)
Burning Image Grails plugin
Posted by admin - 02/09/09 at 01:09:39 amToday I ended my work over my first Grails plug-in: Burning Image. This simple plug-in for images manipulation provides two types of image scaling, and allows to watermark image. In future i want to add crop option and maybe rounding image corner. If you looking for some more informations or you want to try it you could fine it Google code repository.
Groovy closure – will it work?
Posted by admin - 30/08/09 at 04:08:49 pmLets say we have Groovy class like this:
class Foo { def bar(myClosure) { myClosure(this) } private def bas(myName) { println "hello from ${myName}" } }
and I am wonder what should happen if I execute this line of code:
new Foo().bar { it.bas("Steve Zissou") }
Should it throw exception that I want execute private method? But this method is execute inside Foo object instance. So, should it print
hello from Steve Zissou
Unfortunately question is still open because Groovy private method / field bug. Or maybe is time to start using Scala and Lift?
Griffon and table data binding
Posted by admin - 05/05/09 at 04:05:56 pmRecently i have been working on small application in Griffon framewrok. This is MVC framework for desktop applications writed in Groovy - new cool kid in Java World. My first impression was - WOW. Swing builder is my favorite feature immediately after mvc group.
But nothing lasts forever and after few days of work I entered into world of pain. There is no documentation, best practices, or any thing like that. All java developers that were coddled by IDE's like Eclipse will suffer lots of pains because there is no rational support for Groovy. Of course you can hack Netbeans Grails (cousin of Griffon for web applications) plug-in, but after one hour of fight I stayed with my project in Eclipse.
But the hardest thing for me was to menage how to bind data to table. I thinked that there MUST be way to do that by bind statement, but after several hours of searching, I find that there is no such way.
So, how to do it? Use Glazed list.
There is simple example how to use glazed list. This is View from MVC group where we have helper method that create table model. Next we use this method for table creation. In this example model.tableData is instance of ca.odell.glazedlists.BasicEventList.
def createTableModel() { def columnNames = ['col 1', 'col 2', 'col 3', 'col 4', 'col 5'] def variableNames = ['var1', 'var2', 'var3', 'var4', 'var5'] new EventTableModel(model.tableData, [getColumnCount: { columnNames.size() }, getColumnName: { index -> columnNames[index] }, getColumnValue: {object, index -> object."${variableNames[index]}"}] as TableFormat) } panel { borderLayout() scrollPane { table(id:'myTable', selectionMode: ListSelectionModel.SINGLE_SELECTION, autoCreateRowSorter:true, model: createTableModel()) { current.tableHeader.reorderingAllowed = false } } }
If you still looking for more info, check this example for more details.
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.