Closed mjgardner closed 10 years ago
Thanks for the patch. I had not considered required attributes in a role. Sigh..
I'm not a big fan of the second argument to consumer_of
. But, it appears to be backwards-compatible. And the consumer_of
interface doesn't leave many options since %methods
swallows everything.
It would seem there are a couple related problems here, actually. I can't easily call any class methods on the package. And creating multiple instances, requires creating multiple packages.
Something like this would be a work-around:
consumer_of('MyRole')->new();
consumer_of('MyRole')->class_method();
But it's not very clean, and still wouldn't work in your required attribute case.
The common theme here seems to be that consumer_of
should return a class name instead of an instance. That would be backwards incompatible, so I'd rather not make that change. But, what do you think about having a new method instead, that just returned the name of a consuming package?
my $consumer = consuming_class('MyRole')->new(%args);
my $class_name = consuming_class('MyRole');
my @ten_of_em = map { $class_name->new() } 0..9;
$class_name->class_method();
I think a new method would be fine. For bonus points use named parameters so that we don't have the %methods problem.
New method is called consuming_class. I also added a consuming_object for symmetry, both of them use named args.
Roles with attributes or other arguments required at construction need a way to specify those arguments in consumer_of(). This patch adds support for that by checking if the second argument is an arrayref, and if so passing its contents to the instance's new() method if it exists.