Skip to content

Instantly share code, notes, and snippets.

@kiy0taka
Created March 15, 2012 01:17
Show Gist options
  • Save kiy0taka/2040983 to your computer and use it in GitHub Desktop.
Save kiy0taka/2040983 to your computer and use it in GitHub Desktop.
class Person {
String name
static constraints = {
name nullable:false
}
}
Person.metaClass.validate = {
def personInstance = delegate
personInstance.constraints.delegate = personInstance
personInstance.metaClass.methodMissing = { String methodName, args ->
args[0].nullable || personInstance[methodName]
}
personInstance.constraints()
}
assert new Person().validate() == false
assert new Person(name:'kiy0taka').validate() == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment