Skip to content

Instantly share code, notes, and snippets.

@chanwit
Forked from anonymous/Context.groovy
Created July 4, 2013 14:06
Show Gist options
  • Save chanwit/5928096 to your computer and use it in GitHub Desktop.
Save chanwit/5928096 to your computer and use it in GitHub Desktop.
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