Using propertyMissing to enhance Date (in Groovy)
Published by peter December 2nd, 2007 in groovy.In my previous post I had a go at dates and ranges in Groovy. I wasn't to enthusiastic about the fact that it felt a bit verbose and Java-ish. So I took the opportunity to have a go at the new meta-programming stuff in Groovy >= 1.1beta3.
Say I want to be able to do something like this (the properties being Calendar fields):
For me, this seemed like a good case to implement using methodMissing and/or propertyMissing. I came up with the following:
-
split = wrd =~ /([A-Z][a-z]+)/ // collect words
-
-
try{
-
cal.time = delegate // set the time to the current instance
-
}
-
}
-
}
-
-
delegate."get$wrd"()
-
}
Performance-wise (tips, anyone?) this might not be the best solution (the documentation is actually a bit scarce in this point) but now I can remove the dreaded calendar object from my previous code:
Feels better, doesn't it? Now... what about adding a 'parse' and 'format' method to Date... next time ![]()




















0 Responses to “Using propertyMissing to enhance Date (in Groovy)”
Please Wait
Leave a Reply