Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Implementing a Rules DSL


[ 300 ]

binding.allOf = { closure ->
closure.delegate = delegate
def storeResult = binding.result
def storeAnd = binding.and
binding.result = true // Starting premise is true
binding.and = true

closure()

if (storeAnd) {
binding.result = (storeResult && binding.result)
} else {
binding.result = (storeResult || binding.result)
}
binding.and = storeAnd
}

binding.anyOf = { closure ->
closure.delegate = delegate
def storeResult = binding.result
def storeAnd = binding.and

binding.result = false // Starting premise is false
binding.and = false

closure()
if (storeAnd) {
binding.result = (storeResult && binding.result)
} else {
binding.result = (storeResult || binding.result)
}
binding.and = storeAnd
}

binding.grant = { closure ->
closure.delegate = delegate

if (binding.result)
closure()
}
binding.extend = { days ->
def bbPlus = new BroadbandPlus()
bbPlus.extend( binding.account, binding.media, days)
}

http://www.ebook3000.com
Free download pdf