Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where to put in project your own functions?

 
Karol Litwinczuk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
Karol Litwinczuk
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got some good sources on Groovy's metaprogramming?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic