Created
July 4, 2013 14:04
-
-
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+
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