Setting a Default for a managed property


(Grainne Reilly) #1

I am pretty new to Gradle and am trying to write a plugin using the new rule based model configuration.
With Gradle 2.13 there is an example in samples/modelRules/modelDSL with a managed Person and a managed Address. The Address is a property of the Person and it contains a property named city. How can I set a default value, using @Defaults on a method, so that for every person if the city is not set the person.address.city is set to the default value? So, in this example if John Smith did not have his city set to Melbourne it would be set to the default value.
Thanks for any advice.


(Mark Vieira) #2

I think what you’re looking for is @Each.

class PersonRules extends RuleSource {
    @Defaults
    void peopleDefaults(@Each Person person) {
        person.address.city = 'default city'
    }
}

(Grainne Reilly) #3

Thanks, that is precisely what I was looking for.