You should take a look at MooseX::Roles::Parameterized.

- Stevan

On Apr 23, 2009, at 9:43 AM, Mark Morgan wrote:

Good day,

I've got a role, with relevant bits defined as:

-=-
package X::Role::DoesBooleanFilter;

use Moose::Role;

requires 'boolean_filters', 'db_args';

before new => sub {
   my ( $class ) = @_;

# logic to only do the following once, for each class using this role.

   foreach my $filter ( $class->boolean_filters ) {
       my $attributes = {
           is          => 'ro',
           predicate   => "has_$filter",
       };

       $class->meta->add_attribute( $filter => $attributes );
   }
};

# additional methods within the role exist, but not relevant here

1;
-=-

What this is intended to do is add a number of additonal attributes to
the using class, with some common behaviour for each.  The attributes
that are to be added in are defined within the 'boolean_filters'
method of the using class, and are expected to be constant (don't need
to worry about the list of desired attributes changing).

Based on the above, the adding of the attributes should be done for
the entire class once, when the role is first used.  I don't like
having to wrap constructor creation  to handle this, better that there
should be an on-role-included hook that could be tied into.  Does such
exist?  I've not seen anything documented for this type of behaviour.

Cheers,
Mark.

Reply via email to