Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 10

[ 249 ]

The success of building our own builder class by using the Groovy MOP depends
largely on understanding the sequence in which these methods get called. The
output gives us an idea of what might be happening and what the true sequence of
events is. Let's decorate the code a little to show what is happening. The comments
show what scope we are running in:


def method1(Map namedParams = [:], Closure closure = {}) {
println "method1: $namedParams"
closure.call()
}
def method2(Map namedParams = [:], Closure closure = {}) {
println "method2: $namedParams"
closure.call()
}
def method3(Map namedParams = [:], Closure closure = {}) {
println "method3: $namedParams"
closure.call()
}

given:
method1(param: "one") { // Closure1 scope
method2(greet: true) { // Closure2 scope
method3 greeting: "hello"
} // End Closure2
method1( number: 123 ) { // Closure3 scope
method1 ( nestedcall: "nested" ) { // Closure4 scope
method3 number: 10
} // End Closure4
} // End Closure3
} // End Closure1

expect:
"""method1: [param:one]
method2: [greet:true]
method3: [greeting:hello]
method1: [number:123]
method1: [nestedcall:nested]
method3: [number:10]""" == output()
Free download pdf