Groovy web console

subscribe to the feed Subscribe
to this
site
embed in your blog Embed
this
script
Drug regimen DSL (via #groovywebconsole)
tweet this snippet Tweet
this
script

To embed this script in your site, just drop the content below where you want to embed it.

Drug regimen DSL

Published 1 month ago by glaforge with tags dsl
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class Drug {
    String name
    String toString() { name }
}

class DrugQuantity {
    int number
    String toString() {
        number == 1 ? "1 pill" : "$number pills"
    }
}

class TimeDuration {
    Number number
    String unit
}

Integer.metaClass.getPills = { ->  new DrugQuantity(number: delegate) }
Number.metaClass.getHours = { -> new TimeDuration(number: delegate, unit: "hours") }

def take(Map m, DrugQuantity dq) {
    println "Take $dq of $m.of in $m.in.number $m.in.unit"
}

def chloroquinine = new Drug(name: "Chloroquinine")

take 2.pills, of: chloroquinine, in: 6.hours

Recent scripts