Groovy DSL: How I can overload any() in Groovy, when I create internal DSL -
Groovy DSL: How I can overload any() in Groovy, when I create internal DSL -
i create internal dsl , overload any() method defaultgroovymethods.
class rulesprocessor { } live cell fewer 2 live neighbours dies
the lastly line dsl. tried propertymissing, methodmissing, create class, rulesprocessor.metaclass.any, defaultgroovymethods.metaclass.any, doesn't work.
how can write code take dsl? first step 'any' word complicated me.
if can set in closure, delegate object responds any
method or, in example, invokemethod
:
class dsl { def params = [] def invokemethod(string method, args) { params << [method, args] } def propertymissing(string prop) { prop } } = { live cell fewer 2 live neighbours dies } dsl = new dsl() a.delegate = dsl a() assert dsl.params == [ ['any', ['live']], ['cell', ['with']], ['fewer', ['than']], ['two', ['live']], ['neighbours', ['dies']], ]
if reading script file, having method explicitly called any
seems necessary:
import org.codehaus.groovy.control.compilerconfiguration class dsl { def params = [] def invokemethod(string method, args) { params << [method, args] } def any(param) { invokemethod('any', [param]) } def propertymissing(string prop) { prop } } code = 'any live cell fewer 2 live neighbours dies' parsed = new groovyshell( getclass().classloader, new binding(), new compilerconfiguration(scriptbaseclass : delegatingscript.class.name) ).parse( code ) dsl = new dsl() parsed.setdelegate( dsl ) parsed.run() assert dsl.params == [ ['any', ['live']], ['cell', ['with']], ['fewer', ['than']], ['two', ['live']], ['neighbours', ['dies']], ]
kudos mrhaki on compilerconfiguration.
groovy dsl
Comments
Post a Comment