8 package CustomApplication;
11 after apply_methods => sub {
12 my ( $self, $role, $other ) = @_;
13 $self->apply_custom( $role, $other );
18 push @applications, [@_];
23 package CustomApplication::ToClass;
26 with 'CustomApplication';
30 package CustomApplication::ToRole;
33 with 'CustomApplication';
37 package CustomApplication::ToInstance;
40 with 'CustomApplication';
44 package CustomApplication::Composite;
47 with 'CustomApplication';
49 around apply_custom => sub {
50 my ( $next, $self, $composite, $other ) = @_;
51 for my $role ( @{ $composite->get_roles } ) {
52 $self->$next( $role, $other );
58 package CustomApplication::Composite::ToClass;
61 with 'CustomApplication::Composite';
65 package CustomApplication::Composite::ToRole;
68 with 'CustomApplication::Composite';
72 package CustomApplication::Composite::ToInstance;
75 with 'CustomApplication::Composite';
79 package Role::Composite;
82 around apply_params => sub {
83 my ( $next, $self, @args ) = @_;
84 return Mouse::Util::MetaRole::apply_metaclass_roles(
85 for_class => $self->$next(@args),
86 application_to_class_class_roles =>
87 ['CustomApplication::Composite::ToClass'],
88 application_to_role_class_roles =>
89 ['CustomApplication::Composite::ToRole'],
90 application_to_instance_class_roles =>
91 ['CustomApplication::Composite::ToInstance'],
97 package Role::WithCustomApplication;
100 has '+composition_class_roles' => (
101 default => ['Role::Composite'],
107 Mouse::Exporter->setup_import_methods(
108 also => 'Mouse::Role',
112 my ( $self, %options ) = @_;
113 return Mouse::Util::MetaRole::apply_metaclass_roles(
114 for_class => Mouse::Role->init_meta(%options),
115 metaclass_roles => ['Role::WithCustomApplication'],
116 application_to_class_class_roles =>
117 ['CustomApplication::ToClass'],
118 application_to_role_class_roles => ['CustomApplication::ToRole'],
119 application_to_instance_class_roles =>
120 ['CustomApplication::ToInstance'],
126 package My::Role::Normal;
131 package My::Role::Special;
135 ok( My::Role::Normal->meta->isa('Mouse::Meta::Role'), "sanity check" );
136 ok( My::Role::Special->meta->isa('Mouse::Meta::Role'),
137 "using custom application roles does not change the role metaobject's class"
139 ok( My::Role::Special->meta->meta->does_role('Role::WithCustomApplication'),
140 "the role's metaobject has custom applications" );
141 is_deeply( My::Role::Special->meta->composition_class_roles,
143 "the role knows about the specified composition class" );
150 with 'My::Role::Special';
152 ::is( @applications, 1, 'one role application' );
153 ::is( $applications[0]->[0]->name, 'My::Role::Special',
154 "the application's first role was My::Role::Special'" );
155 ::is( $applications[0]->[1]->name, 'Foo',
156 "the application provided an additional role" );
164 with 'My::Role::Special';
166 ::is( @applications, 1 );
167 ::is( $applications[0]->[0]->name, 'My::Role::Special' );
168 ::is( $applications[0]->[1]->name, 'Bar' );
177 My::Role::Special->meta->apply($i);
179 ::is( @applications, 1 );
180 ::is( $applications[0]->[0]->name, 'My::Role::Special' );
181 ::ok( $applications[0]->[1]->is_anon_class );
182 ::ok( $applications[0]->[1]->name->isa('Baz') );
190 with 'My::Role::Normal', 'My::Role::Special';
192 ::is( @applications, 2 );
193 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
194 ::is( $applications[0]->[1]->name, 'Corge' );
195 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
196 ::is( $applications[1]->[1]->name, 'Corge' );
204 with 'My::Role::Normal', 'My::Role::Special';
206 ::is( @applications, 2 );
207 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
208 ::is( $applications[0]->[1]->name, 'Thud' );
209 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
210 ::is( $applications[1]->[1]->name, 'Thud' );
219 Mouse::Meta::Role->combine(
220 [ 'My::Role::Normal' => undef ],
221 [ 'My::Role::Special' => undef ],
224 ::is( @applications, 2 );
225 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
226 ::ok( $applications[0]->[1]->is_anon_class );
227 ::ok( $applications[0]->[1]->name->isa('Garply') );
228 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
229 ::ok( $applications[1]->[1]->is_anon_class );
230 ::ok( $applications[1]->[1]->name->isa('Garply') );