My favorites
▼
|
Sign in
rintcius
Miscellaneous code
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
rintcius.groovy
/
src
/
main
/
groovy
/
nl
/
rintcius
/
groovy
/
ExpandoDsl.groovy
r3
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()
}
}
Show details
Hide details
Change log
r3
by rintcius on Sep 23, 2009
Diff
Initial version of ExpandoBuilder and ExpandoDsl
Go to:
/trunk/rintcius.groovy
/trunk/rintcius.groovy/.classpath
/trunk/rintcius.groovy/.project
/trunk/rintcius.groovy/.settings
...groovy.eclipse.preferences.prefs
...tings/org.eclipse.jdt.core.prefs
...ings/org.maven.ide.eclipse.prefs
/trunk/rintcius.groovy/pom.xml
/trunk/rintcius.groovy/src
/trunk/rintcius.groovy/src/main
.../rintcius.groovy/src/main/groovy
...ntcius.groovy/src/main/groovy/nl
...oovy/src/main/groovy/nl/rintcius
...c/main/groovy/nl/rintcius/groovy
...ius/groovy/ExpandoBuilder.groovy
...intcius/groovy/ExpandoDsl.groovy
/trunk/rintcius.groovy/src/test
.../rintcius.groovy/src/test/groovy
...ntcius.groovy/src/test/groovy/nl
...oovy/src/test/groovy/nl/rintcius
...c/test/groovy/nl/rintcius/groovy
...groovy/ExpandoBuilderTest.groovy
...ius/groovy/ExpandoDslTest.groovy
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 1526 bytes, 66 lines
View raw file
Powered by
Google Project Hosting