Skip to content

Instantly share code, notes, and snippets.

Created July 4, 2013 14:04
Show Gist options
  • Save anonymous/5928075 to your computer and use it in GitHub Desktop.
Save anonymous/5928075 to your computer and use it in GitHub Desktop.
A simple DCI implementation in Groovy using instance-based Mixins and poorman DSL. Work only with Groovy 2.1+
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