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
def mongoContext = context(
        ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*/))
)
contributor(mongoContext) {
    def path = ""
    try {
        path = psiClass.containingFile.originalFile.virtualFile.path
    } catch (Exception e) {
    }
    if (path =~ ".*/*grails-app/mongoDomain/.*") {
        classType?.fields?.each {
            if ((it.name == "belongsTo") || (it.name == "hasOne")) {
                it.children.each {child ->
                    List definitions = child.text.toString().replaceAll(/\[|\]/, "").trim().tokenize(",")*.trim()
                    definitions.each {
                        List tokens = it.tokenize(":")*.trim()
                        property(name: tokens.first().toString(), type: tokens.last().toString())
                    }
                }
            }
        }
        classType?.fields?.each {
            if (it.name == "hasMany") {
                it.children.each {child ->
                    List definitions = child.text.toString().replaceAll(/\[|\]/, "").trim().tokenize(",")*.trim()
                    definitions.each {
                        List tokens = it.tokenize(":")*.trim()
                        property(name: tokens.first().toString(), type: "List<${tokens.last().toString()}>")
                    }
                }
            }
        }
        //Adding Properties
        property(name: "id", type: ‘org.bson.types.ObjectId’)
        property(name: "mongoVersion", type: ‘Integer’)
        property(name: "version", type: ‘Integer’)
        property(name: "log", type: ‘org.apache.log4j.Logger’)
        property(name: "clazz", type: ‘Class’)
        property(name: ‘errors’, type: ‘Map’)
    }
}
[/java]
Read Further in the “GDSL AWESOMENESS” Series
- GDSL Awesomeness – Introduction to GDSL in IntelliJ Idea
- GDSL Awesomeness – Understanding Context And Contributors in Detail
- GDSL Awesomeness – Defining dynamic property in a class
- GDSL Awesomeness – Defining dynamic method in a class
- GDSL Awesomeness – Adding Hinting for missingMethod implementation
- GDSL Awesomeness – Setting a global context
- GDSL Awesomeness – Delegating Closure Calls
- GDSL Awesomeness – Defining methods in a closure passed to method of particular name
- GDSL Awesomeness – Defining Properties in a closure passed to method of particular name
- GDSL Awesomeness – Contributing to classes which match a path/name pattern
- GDSL Awesomeness – contributing methods with current class return types or parameters
- GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure
- GDSL Awesomeness – Getting Code of a method or Field in GDSL Script
- GDSL Awesomeness – Advanced Example showing domain properties implementation
Hope It Helped.
Kushal Likhi
 
     
					 
							