-
Notifications
You must be signed in to change notification settings - Fork 10
Closures
When a function is not expected to be used more than once and is simple enough to fit in one statement, you can define such a validator or a converter as a closure, for example:
guestsNum toInt >> {it > 1}
The it
variable inside the closure refers to a value passed from a previous subrule (here toInt
). Also, since closures are part of the rules you have an access to all input parameters, e.g.:
adminsNum toInt
guestsNum toInt >> {guestsNum + adminsNum < maxUsersNum}
You can also use a closure validator or converter to explicitly define all function parameters, for example, to run a function against a parameter that is not the default for a current rule.
Validator and converter calls that occur in a closure body are handled as general Groovy method calls and thus all function arguments must be specified. For example:
// expectedChildrenNum is a method that takes one argument
expectedGuestsNum toInt >> {it + expectedChildrenNum(it)}
Using a closure you can easily assign a constant value to a parameter, for example:
tempParameter {7}