aspose file tools*
The moose likes Groovy and the fly likes Where to put in project your own functions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Where to put in project your own functions?" Watch "Where to put in project your own functions?" New topic
Author

Where to put in project your own functions?

Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
I now it's total newbie question but cant't find answer on quick search in documnataion or in Google - where should I put mine own, general functions that i reuse in app? I write special a function to change empty strings in to strings of '-' and want to use them many, many, MANY times in app, so the "copy-paste" method for each Controller is illogical. Should all functions goes to Scripts catalog or in to other place?
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
You have a couple of options here, I tend to place reusable code that doesn't belong in Domain classes/Controllers or Services into it's own class in either the src/groovy or src/java folder depending on whether its groovy or pure java code obviously.

Alternatively for you specific task, have you considered the use of meta-programming? That is you can create a function that becomes part of the String class, so you would be able to call your function on any String instance.

I had a similar requirement where I needed a 'truncate' method so Strings over a certain length would be returned with a trailing "..." after a specified number of characters.

Have a look at my tiny post on how to accomplish it..

http://www.grailsforum.co.uk/showthread.php/17-Why-you-shouldn-t-NOT-learn-about-meta-programming.?p=30#post30

Rgds,


Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Karol Litwinczuk
Greenhorn

Joined: Jun 11, 2010
Posts: 13
Got some good sources on Groovy's metaprogramming?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to put in project your own functions?
 
Similar Threads
Hierarchical MVC?
how to use javascript function in struts
Two instances of Tomcat 5.5.17
custom javascript function in struts
Accessing parameter from web.xml inside a MessageResource class