My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package nl.rintcius.groovy

public class ExpandoDsl {

Script script
Binding binding
boolean setRootProperty = false

public ExpandoDsl(String scriptText) {
this(new GroovyShell().parse(scriptText))
}

public ExpandoDsl(Script script) {
this.script = script
this.binding = script.binding
Expando root = new Expando()
this.binding.setVariable("root", root)
List stack = [root]
this.binding.setVariable("stack", stack)
}

public Expando create() {
def invokeMethod = { String name, args ->

def metaMethod = delegate.metaClass.getMetaMethod(name, args)
def result
if (metaMethod) {
result = metaMethod.invoke(delegate,args)
} else {
Expando e = new Expando()
List stack = binding.getVariable("stack")
Expando parent = stack.last()
assert parent != null
parent."$name" = e
for (arg in args) {
if (arg instanceof Closure) {
Closure cl = arg
cl.delegate = e
cl.resolveStrategy = Closure.DELEGATE_FIRST
stack.push(e)
if (setRootProperty) {
cl.setProperty("root", binding.getVariable("root"))
}
cl.call()
stack.pop()
} else if (arg instanceof Map) {
arg.each() { k, v ->
e."$k" = v
}

} else {
e.value = arg
if (!e.valueList) {
e.valueList = []
}
e.valueList << arg
}
}
result = stack.last()
}
result
}
script.metaClass.invokeMethod = invokeMethod
return script.run()
}
}

Change log

r3 by rintcius on Sep 23, 2009   Diff
Initial version of ExpandoBuilder and
ExpandoDsl
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1526 bytes, 66 lines
Powered by Google Project Hosting