• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Where to put in project your own functions?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got some good sources on Groovy's metaprogramming?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic