I have a category class that I want to “Use” that acts on a third-party service class. The service class has a bunch of xxxRequest and xxxResponse classes plus additional child classes for data for each xxx call.
I want to automatically apply categories to each of the additional classes at the same time as the service category is applied, with the same scope.
I could list all the category classes in the use block but there are a lot and I don’t want to expose that detail. The base classes are out of my control so I can’t use @category/@mixin.
Currently I have a static initialise method in my service category that does a series of mixin calls (as well as adding the odd extra constructor). This works ok but doesn’t provide the scoping I want.
Is there an easier way that I have missed?
I’m not sure I understand what you are trying to do. If you are trying to
usemany categories at once in the same block of code but you don’t want to list all those categories each time you want to use them (please correct me if that is not what you meant), you can put that longuseinside a method that takes a block of code (a Closure) as a parameter and then use that method instead of the longuse. Something like:In this silly example, the String and Integer classes would be the classes that you want to augment but you don’t have access to, and the
useAwesomeExtensionsacts like a big category that includes a bunch of other categories. Hope that helps 🙂