I have a Moose::Role
that I would like to call some extra subs on the class when that role is applied to the class.
Is there an easy way to modify what happens when the role is applied, without having to dig too much into Moose::Meta::Role
type coding? Ideally, I'd just like to after 'apply' => ...
to add the extra stuff.
Edit:
I'm specifically using this with a DBIx::Class::Core
result definition to create something like a component that also modifies the constructor. I would just write it as a component if I could get at BUILDARGS
and BUILD
subs for the result, but I can't seem to do. So, instead of doing load_component
, I doing with 'role'
, but some of the effects of the component are to add belongs_to
relationships to the class. Hence, I was thinking the best way to do that is during application of the role to the class.
What I found that works, is compact, and seems in keeping with intent in the docs is to use a trait to modify the meta role used by my particular role:
package DBIx::Class::Meta::Role::MyRole;
use Moose;
BEGIN { extends 'Moose::Meta::Role'; }
after 'apply' => sub {
## ..my mods to add extra relationships to DBIx::Class::Core result
};
no Moose;
package DBIx::Class::MyRole;
use Moose::Role -metaclass => 'DBIx::Class::Meta::Role::MyRole';