Skip to content

Instantly share code, notes, and snippets.

@kaakaa
Created January 24, 2014 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaakaa/8599348 to your computer and use it in GitHub Desktop.
Save kaakaa/8599348 to your computer and use it in GitHub Desktop.
def dsl = '''
person.is {
name 'taro'
age '28'
}.say()
'''
def impl = '''
def getPerson(){
new Person()
}
class Person{
def Person(){
def mc = new ExpandoMetaClass( Person, false, true)
mc.initialize()
this.metaClass = mc
}
def is(Closure closure){
closure.delegate = this
closure()
return this
}
def methodMissing(String name, args){
this.metaClass."${name}" = args[0]
}
def say(){
println "I am ${this.name}. ${this.age} old."
}
}
'''
def script = """
${dsl}
${impl}
"""
new GroovyShell().evaluate(script)
@kaakaa
Copy link
Author

kaakaa commented Jan 24, 2014

I am taro. 28 old.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment