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 Moose::Util::MetaRole::apply_metaroles(
85 for => $self->$next(@args),
87 application_to_class =>
88 ['CustomApplication::Composite::ToClass'],
89 application_to_role =>
90 ['CustomApplication::Composite::ToRole'],
91 application_to_instance =>
92 ['CustomApplication::Composite::ToInstance'],
99 package Role::WithCustomApplication;
102 around composition_class_roles => sub {
103 my ($orig, $self) = @_;
104 return $self->$orig, 'Role::Composite';
110 Moose::Exporter->setup_import_methods(
111 also => 'Moose::Role',
115 my ( $self, %options ) = @_;
116 return Moose::Util::MetaRole::apply_metaroles(
117 for => Moose::Role->init_meta(%options),
119 role => ['Role::WithCustomApplication'],
120 application_to_class =>
121 ['CustomApplication::ToClass'],
122 application_to_role => ['CustomApplication::ToRole'],
123 application_to_instance =>
124 ['CustomApplication::ToInstance'],
131 package My::Role::Normal;
136 package My::Role::Special;
140 ok( My::Role::Normal->meta->isa('Moose::Meta::Role'), "sanity check" );
141 ok( My::Role::Special->meta->isa('Moose::Meta::Role'),
142 "using custom application roles does not change the role metaobject's class"
144 ok( My::Role::Special->meta->meta->does_role('Role::WithCustomApplication'),
145 "the role's metaobject has custom applications" );
146 is_deeply( [My::Role::Special->meta->composition_class_roles],
148 "the role knows about the specified composition class" );
155 with 'My::Role::Special';
157 ::is( @applications, 1, 'one role application' );
158 ::is( $applications[0]->[0]->name, 'My::Role::Special',
159 "the application's first role was My::Role::Special'" );
160 ::is( $applications[0]->[1]->name, 'Foo',
161 "the application provided an additional role" );
169 with 'My::Role::Special';
171 ::is( @applications, 1 );
172 ::is( $applications[0]->[0]->name, 'My::Role::Special' );
173 ::is( $applications[0]->[1]->name, 'Bar' );
182 My::Role::Special->meta->apply($i);
184 ::is( @applications, 1 );
185 ::is( $applications[0]->[0]->name, 'My::Role::Special' );
186 ::ok( $applications[0]->[1]->is_anon_class );
187 ::ok( $applications[0]->[1]->name->isa('Baz') );
195 with 'My::Role::Normal', 'My::Role::Special';
197 ::is( @applications, 2 );
198 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
199 ::is( $applications[0]->[1]->name, 'Corge' );
200 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
201 ::is( $applications[1]->[1]->name, 'Corge' );
209 with 'My::Role::Normal', 'My::Role::Special';
211 ::is( @applications, 2 );
212 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
213 ::is( $applications[0]->[1]->name, 'Thud' );
214 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
215 ::is( $applications[1]->[1]->name, 'Thud' );
224 Moose::Meta::Role->combine(
225 [ 'My::Role::Normal' => undef ],
226 [ 'My::Role::Special' => undef ],
229 ::is( @applications, 2 );
230 ::is( $applications[0]->[0]->name, 'My::Role::Normal' );
231 ::ok( $applications[0]->[1]->is_anon_class );
232 ::ok( $applications[0]->[1]->name->isa('Garply') );
233 ::is( $applications[1]->[0]->name, 'My::Role::Special' );
234 ::ok( $applications[1]->[1]->is_anon_class );
235 ::ok( $applications[1]->[1]->name->isa('Garply') );