ok having thought about this some more i think i know how we can do
it..basically we have a closure property called "constraints"
@Property constraints = {}
we intercept a call to either getProperty("constraints") and get it to
execute the closure rather than returning it
before executing the closure we temporarily change the metaclass of
the domain object for the scope of the closure call (we set it back to
the original in the after() call of the interceptor)
We use a proxy metaclass to intercept all calls to properties on the
domain class and wrap them in a ConstrainProperty class before
returning them (they will be created on the first call to "this.login"
for example)
At the end of the closure a list of ConstrainProperty instances will
have been built up and returned by the closure (after setting the
metaclass back to the original)
The "validate()" closure then uses these and you can also loop over
them for example:
@Property validate = {
this.constraints.each {
println it.property
println it.minLength
// etc
}
}
Graeme
On 30/10/05, Dierk Koenig <
[hidden email]> wrote:
> > > login.maxLength = 16
> > > could be done with a ProxyMetaClass/Interceptor that intercepts all
> > > method calls of well-known names (e.g.setMaxLength).
> > > But I'm not sure it's worth the effort.
> >
> > It maybe be worth the effort as i prefer the syntax but my concern is
> > that a String is not a GroovyObject it gets its MetaClass from the
> > registry if i'm not mistaken..
>
> ProxyMetaClass currently intercepts only method calls to a
> well-known class (this can also be java.lang.String).
> But that could be easily extended for our purposes
> to a list of classes, a class-name pattern, or all classes.
> It would actually work even with the existing ProxyMetaClass but
> would require constructing a new one around each method call :-(
>
> > but as I said if you have another idea how we could implement this let
> > me know as I do prefer the syntax:
> >
> > this.login.maxLength = 10
> >
> > As it seems more like you're setting it against the property.
>
> Yes, cool and much easier. +1
>
> Mittie
>
>
>