docdurdee has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, I'm using the MooseX::ClassCompositor to test some roles (following a response jandrew to How to test a Moose Role?). Is there a way to inject role requirements? I suspect it is in the class_metaroles attribute... i have not yet delved into the meta.
my $class = MooseX::ClassCompositor->new( {class_basename => 'Test'} )->class_for('Foo');
-----------_build_bar in the Role below------------
package Foo; Moose:Role; requires '_build_bar'; no Moose::Role; 1;

Replies are listed 'Best First'.
Re: MooseX::ClassCompositor for roles with requirements
by tobyink (Canon) on Aug 13, 2013 at 19:56 UTC

    There's nothing built-in, but you can create a subclass of MooseX::ClassCompositor that does the job in about a dozen lines of code...

    { package MooseX::ClassCompositor::OnAcid; use Moose; extends qw( MooseX::ClassCompositor ); around class_for => sub { my $orig = shift; my $self = shift; my @roles = map { ref($_) eq q(HASH) ? 'Moose::Meta::Role'->create_anon_role +(methods => $_) : $_ } @_; $self->$orig(@roles); }; } { package Foo; use Moose::Role; requires '_build_bar'; } my %methods = ( _build_bar => sub {"foo"}, ); my $class = MooseX::ClassCompositor::OnAcid->new( { class_basename => 'Test' } )->class_for('Foo', \%methods); print $class->new->dump;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      It works! Thanks.