The closure for my StreamingMarkupBuilder is very large, so I would like to
refactor it into smaller units. What is the recommended way to do this?
I came across
https://slideguitarist.blogspot.com/2011/09/refactoring-groovy-to-generate-xml.html
but hoping there is a more elegant way.
This is my sample:
def transactions = [ [ changeFlag: 'I', name:'Pete']
, [ changeFlag: 'U', name:'John']
]
Closure c = {
invoices {
transactions.each { trx ->
if (trx.changeFlag=='I') {
line(type:'New'){
name(trx.name)
}
} else {
line(type:'Update'){
name(trx.name)
}
}
}
}
}
def builder = new groovy.xml.StreamingMarkupBuilder()
def writable = builder.bind c
def result = writable.toString()
println result
And I would like to refactor this into something resembling:
def transactions = [ [ changeFlag: 'I', name:'Pete']
, [ changeFlag: 'U', name:'John']
]
def xmlInsert (trx) {
line(type:'New'){
name(trx.name)
}
}
def xmlUpdate (trx) {
line(type:'Update'){
name(trx.name)
}
}
Closure c = {
invoices {
transactions.each { trx ->
if (trx.changeFlag=='I') {
xmlInsert trx
} else {
xmlUpdate trx
}
}
}
}
def builder = new groovy.xml.StreamingMarkupBuilder()
def writable = builder.bind c
println writable.toString()
This code results in: groovy.lang.MissingMethodException: No signature of
method: invoices() is applicable