3
votes

I inserted this line into my init() of my BootStrap class

Integer.metaClass.minutes = { 60000L * delegate }

I was then not able to use it from a Job class (Quartz plugin). Do I put this line of code somewhere else to make it a global modification?

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?

1
You could use the already existing: def time = use( TimeCategory ) { 10.minutes.toMilliseconds() }... Although this doesn't answer your question ;-) - tim_yates
very cool. Can you explain the use and TimeCategory thing? Best reference on it? Awesome +1 on the comment - BuddyJoe
@tyndall TimeCategory is a Groovy Category. The use function can be thought of as a way to temporarially add functionality to a class, so inside the use(){ ... } block, the Category is in effect and integers are decorated with time based properties. Similar to adding a method to the metaClass, but more localised. - tim_yates
NB: In groovy 1.8, TimeCategory has moved to the groovy.time.* package, which shouldn't affect things until Grails starts using it (v1.4?) - tim_yates
@tim_yates actually, TimeCategory exists in the the new location in groovy 1.7. It should be imported from there even with grails 1.3. - ataylor

1 Answers

1
votes

Do I put this line of code somewhere else to make it a global modification?

Use the DelegatingMetaClass

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?

If you want the function to be an instance method of all classes, then you must add it to the metaClass of Object (see above). If not, simply add the function as a static method of a class, i.e. the same way you make functions globally accessible in Java.