-
-
Save chanwit/5928096 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FromRole { | |
static cap(String self) { | |
self.toUpperCase() | |
} | |
} | |
class ToRole { | |
static uncap(String self) { | |
self.toLowerCase() | |
} | |
} | |
class Context { | |
String from | |
String to | |
void setFrom(String value) { | |
this.from = value | |
this.from.metaClass.mixin FromRole | |
} | |
void setTo(String value) { | |
this.to = value | |
this.to.metaClass.mixin ToRole | |
} | |
def method() { | |
println "from ${from.cap()} to ${to.uncap()}" | |
} | |
} | |
def Context = { c -> | |
return [c: c, context: new Context()] | |
} | |
def put = { map -> | |
return [into: { x -> | |
x.c.resolveStrategy = Closure.DELEGATE_FIRST | |
map.each {k, v -> | |
x.context["$k"] = v | |
} | |
x.c.delegate = x.context | |
x.c.call() | |
}] | |
} | |
// use Context | |
put from:"a", to:"B" into Context { | |
method() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment