Grails

GDSL Awesomeness – Defining methods in a closure passed to method of particular name

hi,   Sometimes we need to add specific methods in the closures which are passed to methods with a particular name. This can be simply done as follows   Suppose we need to add the method named "setHeaders(Map headers)" in all closures passed to method with name "withREST" [java] //Get the context for all closures def...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Defining Properties in a closure passed to method of particular name

hi,   Sometimes we need to add specific properties in the closures which are passed to methods with a particular name. This can be simply done as follows   Suppose we need to add the property named requestMethod of type String in all closures passed to method with name "withREST" [java] //Get the context for all...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Contributing to classes which match a path/name pattern

hi,   If we want to contribute to classes which matches a particular path/name pattern, then we can use PSI Class patterns to do it.   the code is as follows: [java] import com.intellij.patterns.PsiJavaPatterns import com.intellij.patterns.PlatformPatterns //get the context of all files, and enable path matching....

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – contributing methods with current class return types or parameters

Hi,   Sometimes we need to contribute dynamically using current target class type.   Lets take the example of .get() and .list() methods of Grails domain class. The signature of each method is as follows: public <DomainClassType> get(Long id) , Example: DemoClass.get(1) will return an object of "DemoClass" ...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure

Hi,   I have been using the Grails mail plugin for a while now, this plugin injects a method named "sendMail" to all controllers and services.   Our aim will be to provide auto-completes for this method and the closure inside.   The GDSL Script is as follows: [java] import com.intellij.patterns.PsiJavaPatterns ...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Getting Code of a method or Field in GDSL Script

Hi,   Here i will illustrate how you can get the code of a method within GDSL Script:   The script is as follows: [java] import com.intellij.patterns.PsiJavaPatterns import com.intellij.patterns.PlatformPatterns def mongoContext = context( ctype:...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

GDSL Awesomeness – Advanced Example showing domain properties implementation

Hi,   Here is an example which uses a lot of gdsl concepts. This example tries to simulate injecting properties to Domains.   The Example is as follows: (The code is self explanatory and can be used as a reference) [java] import com.intellij.patterns.PsiJavaPatterns import com.intellij.patterns.PlatformPatterns ...

by Kushal Likhi
Tag: Groovy
24-Sep-2012

Grails

MetaProgramming with MetaClass

As we all know that groovy is a dynamic programming language, so we can add any method to any class at runtime. First, let’s see a small and simple example of metaProgamming given below, in which we are adding a method (isEvent) into an Integer class. [groovy] Integer.metaClass.isEven = { -> // only (->) sign indicates that...

by Amit Kumar
Tag: Groovy
24-Sep-2012

Grails, Product Engineering

Copy Files/Folders from one location to another in Groovy

It is normally difficult to copy file/folders from one location to another. In my recent Grails Development project, i successfully achieved this. I had to copy files/folders from one location to another on file system. I searched about it and came across a cool method to copy files or directories from one location to another. We can...

by Vishal Sahu
Tag: Groovy
21-Sep-2012

Grails

Understanding Groovy 2.0 withLazyDefault and withEagerDefault methods

Sometimes there is need to get some default value in place of null values from a list, that was the reason I came across: withLazyDefault and withEagerDefault methods. My use case consisted of a list of user names with possibility of null elements, hence wherever the element was null, I wanted to return “unknown”. Although there...

by Divya Setia
Tag: Groovy
17-Sep-2012

Grails

Implement Ajax call in Grails web-flow

In one of my recent project, i want to use grails web-flow with ajax call. It's very easy to implement the web-flow with ajax call. Grails web-flow always track the actions on the basis of eventId & flow execution key. So, to implement ajax call in web-flow, we have to pass the event id & flow execution key. 1. Let us assume,...

by Mohit Garg
Tag: Groovy
16-Sep-2012

Grails

Two Interesting methods in Groovy 2.0 : takeWhile and dropWhile

Today I found two very interesting methods: takeWhile and dropWhile. The names itself reveals their work i.e. takeWhile (consider while some condition is true) and dropWhile (ignore while some condition is true). Although, these methods have been added to Groovy 1.8.7, but for character sequence, these methods are supported in Groovy...

by Divya Setia
Tag: Groovy
12-Sep-2012