Fix compatibility issue on Ark
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
CommitLineData
a2227e71 1package Mouse::Meta::Role;
2use strict;
3use warnings;
74be9f76 4
c6d8f5ca 5use Mouse::Util qw(:meta not_supported english_list get_code_info);
6d28c5cf 6use Mouse::Meta::Module;
f3bb863f 7our @ISA = qw(Mouse::Meta::Module);
a2227e71 8
6cfa1e5e 9sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
10
c6d8f5ca 11# XXX: for backward compatibility
12my %foreign = map{ $_ => undef } qw(Mouse::Role Carp Scalar::Util UNIVERSAL);
13sub _code_is_mine{
14 my($self, $code) = @_;
15
16 my($package, $name) = get_code_info($code);
17
18 return $package && !exists $foreign{$package};
19}
20
21
8e64d0fa 22sub _construct_meta {
acf0f643 23 my $class = shift;
7a50b450 24
acf0f643 25 my %args = @_;
26
3a63a2e7 27 $args{methods} ||= {};
59089ec3 28 $args{attributes} ||= {};
29 $args{required_methods} ||= [];
47f36c05 30 $args{roles} ||= [];
274b6cce 31
7a50b450 32# return Mouse::Meta::Class->initialize($class)->new_object(%args)
33# if $class ne __PACKAGE__;
34
8e64d0fa 35 return bless \%args, ref($class) || $class;
7a50b450 36}
37
38sub create_anon_role{
39 my $self = shift;
40 return $self->create(undef, @_);
41}
42
43sub is_anon_role{
44 return exists $_[0]->{anon_serial_id};
acf0f643 45}
a2227e71 46
afc73948 47sub get_roles { $_[0]->{roles} }
48
6cfa1e5e 49sub get_required_method_list{
50 return @{ $_[0]->{required_methods} };
51}
afc73948 52
59089ec3 53sub add_required_methods {
ea249879 54 my($self, @methods) = @_;
59089ec3 55 push @{$self->{required_methods}}, @methods;
56}
57
6cfa1e5e 58sub requires_method {
59 my($self, $name) = @_;
60 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
61}
62
274b6cce 63sub add_attribute {
64 my $self = shift;
65 my $name = shift;
6cfa1e5e 66
67 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
da0c885d 68}
69
7a50b450 70sub _canonicalize_apply_args{
71 my($self, $applicant, %args) = @_;
72
73 if($applicant->isa('Mouse::Meta::Class')){
74 $args{_to} = 'class';
75 }
76 elsif($applicant->isa('Mouse::Meta::Role')){
77 $args{_to} = 'role';
78 }
79 else{
80 $args{_to} = 'instance';
81
82 not_supported 'Application::ToInstance';
83 }
84
85 if($args{alias} && !exists $args{-alias}){
86 $args{-alias} = $args{alias};
87 }
88 if($args{excludes} && !exists $args{-excludes}){
89 $args{-excludes} = $args{excludes};
90 }
91
92 if(my $excludes = $args{-excludes}){
93 $args{-excludes} = {}; # replace with a hash ref
94 if(ref $excludes){
95 %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
96 }
97 else{
98 $args{-excludes}{$excludes} = undef;
99 }
100 }
101
102 return \%args;
103}
104
3a63a2e7 105sub _check_required_methods{
106 my($role, $class, $args, @other_roles) = @_;
107
7a50b450 108 if($args->{_to} eq 'class'){
3a63a2e7 109 my $class_name = $class->name;
7a50b450 110 my $role_name = $role->name;
111 my @missing;
3a63a2e7 112 foreach my $method_name(@{$role->{required_methods}}){
7a50b450 113 if(!$class_name->can($method_name)){
3a63a2e7 114 my $has_method = 0;
115
116 foreach my $another_role_spec(@other_roles){
117 my $another_role_name = $another_role_spec->[0];
118 if($role_name ne $another_role_name && $another_role_name->can($method_name)){
119 $has_method = 1;
120 last;
121 }
122 }
7a50b450 123
124 push @missing, $method_name if !$has_method;
3a63a2e7 125 }
126 }
7a50b450 127 if(@missing){
128 $class->throw_error("'$role_name' requires the "
129 . (@missing == 1 ? 'method' : 'methods')
130 . " "
131 . english_list(map{ sprintf q{'%s'}, $_ } @missing)
132 . " to be implemented by '$class_name'");
133 }
134 }
135 elsif($args->{_to} eq 'role'){
136 # apply role($role) to role($class)
137 foreach my $method_name($role->get_required_method_list){
138 next if $class->has_method($method_name); # already has it
139 $class->add_required_methods($method_name);
140 }
3a63a2e7 141 }
2e92bb89 142
3a63a2e7 143 return;
2e92bb89 144}
145
3a63a2e7 146sub _apply_methods{
147 my($role, $class, $args) = @_;
da0c885d 148
3a63a2e7 149 my $role_name = $role->name;
150 my $class_name = $class->name;
6cfa1e5e 151
7a50b450 152 my $alias = $args->{-alias};
153 my $excludes = $args->{-excludes};
e0b163e1 154
3a63a2e7 155 foreach my $method_name($role->get_method_list){
156 next if $method_name eq 'meta';
157
158 my $code = $role_name->can($method_name);
6cfa1e5e 159
7a50b450 160 if(!exists $excludes->{$method_name}){
6cfa1e5e 161 if(!$class->has_method($method_name)){
162 $class->add_method($method_name => $code);
163 }
2e92bb89 164 }
2e92bb89 165
3a63a2e7 166 if($alias && $alias->{$method_name}){
167 my $dstname = $alias->{$method_name};
6cfa1e5e 168
7a50b450 169 my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} };
170
171 if(defined($dstcode) && $dstcode != $code){
6cfa1e5e 172 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
21498b08 173 }
3a63a2e7 174 else{
175 $class->add_method($dstname => $code);
2e92bb89 176 }
59089ec3 177 }
178 }
179
3a63a2e7 180 return;
181}
182
183sub _apply_attributes{
184 my($role, $class, $args) = @_;
185
7a50b450 186 if ($args->{_to} eq 'class') {
b1b81553 187 # apply role to class
3a63a2e7 188 for my $attr_name ($role->get_attribute_list) {
189 next if $class->has_attribute($attr_name);
190
191 my $spec = $role->get_attribute($attr_name);
05b9dc92 192
1b9e472d 193 $class->add_attribute($attr_name => %{$spec});
b1b81553 194 }
7a50b450 195 }
196 elsif($args->{_to} eq 'role'){
b1b81553 197 # apply role to role
3a63a2e7 198 for my $attr_name ($role->get_attribute_list) {
199 next if $class->has_attribute($attr_name);
200
201 my $spec = $role->get_attribute($attr_name);
202 $class->add_attribute($attr_name => $spec);
b1b81553 203 }
da0c885d 204 }
d99db7b6 205
3a63a2e7 206 return;
207}
208
209sub _apply_modifiers{
210 my($role, $class, $args) = @_;
211
7a50b450 212 for my $modifier_type (qw/override before around after/) {
3a63a2e7 213 my $add_modifier = "add_${modifier_type}_method_modifier";
214 my $modifiers = $role->{"${modifier_type}_method_modifiers"};
d99db7b6 215
3a63a2e7 216 while(my($method_name, $modifier_codes) = each %{$modifiers}){
6cfa1e5e 217 foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
3a63a2e7 218 $class->$add_modifier($method_name => $code);
d99db7b6 219 }
220 }
221 }
3a63a2e7 222 return;
da0c885d 223}
0fc8adbc 224
3a63a2e7 225sub _append_roles{
226 my($role, $class, $args) = @_;
21498b08 227
7a50b450 228 my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles;
3a63a2e7 229
230 foreach my $r($role, @{$role->get_roles}){
231 if(!$class->does_role($r->name)){
232 push @{$roles}, $r;
21498b08 233 }
234 }
3a63a2e7 235 return;
236}
21498b08 237
3a63a2e7 238# Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
239sub apply {
7a50b450 240 my $self = shift;
241 my $applicant = shift;
3a63a2e7 242
7a50b450 243 my $args = $self->_canonicalize_apply_args($applicant, @_);
21498b08 244
7a50b450 245 $self->_check_required_methods($applicant, $args);
246 $self->_apply_methods($applicant, $args);
247 $self->_apply_attributes($applicant, $args);
248 $self->_apply_modifiers($applicant, $args);
249 $self->_append_roles($applicant, $args);
3a63a2e7 250 return;
251}
21498b08 252
3a63a2e7 253sub combine_apply {
254 my(undef, $class, @roles) = @_;
05b9dc92 255
7a50b450 256 if($class->isa('Mouse::Object')){
257 not_supported 'Application::ToInstance';
258 }
259
60b5c3be 260 # check conflicting
261 my %method_provided;
262 my @method_conflicts;
263 my %attr_provided;
264 my %override_provided;
265
266 foreach my $role_spec (@roles) {
267 my $role = $role_spec->[0]->meta;
268 my $role_name = $role->name;
269
270 # methods
271 foreach my $method_name($role->get_method_list){
272 next if $class->has_method($method_name); # manually resolved
273
274 my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } };
275
276 my $c = $method_provided{$method_name};
277
278 if($c && $c->[0] != $code){
279 push @{$c}, $role;
280 push @method_conflicts, $c;
281 }
282 else{
283 $method_provided{$method_name} = [$code, $method_name, $role];
284 }
285 }
286
287 # attributes
288 foreach my $attr_name($role->get_attribute_list){
289 my $attr = $role->get_attribute($attr_name);
290 my $c = $attr_provided{$attr_name};
291 if($c && $c != $attr){
8e64d0fa 292 $class->throw_error("We have encountered an attribute conflict with '$attr_name' "
60b5c3be 293 . "during composition. This is fatal error and cannot be disambiguated.")
294 }
295 else{
296 $attr_provided{$attr_name} = $attr;
297 }
298 }
299
300 # override modifiers
301 foreach my $method_name($role->get_method_modifier_list('override')){
302 my $override = $role->get_override_method_modifier($method_name);
303 my $c = $override_provided{$method_name};
304 if($c && $c != $override){
8e64d0fa 305 $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
306 . "composition (Two 'override' methods of the same name encountered). "
60b5c3be 307 . "This is fatal error.")
308 }
309 else{
310 $override_provided{$method_name} = $override;
311 }
312 }
313 }
314 if(@method_conflicts){
315 my $error;
316
317 if(@method_conflicts == 1){
318 my($code, $method_name, @roles) = @{$method_conflicts[0]};
319 $class->throw_error(
320 sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'},
321 english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name
322 );
323 }
324 else{
325 @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs
326 my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts);
327 my $roles = english_list(
328 map{ sprintf q{'%s'}, $_->name }
329 map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts
330 );
331
332 $class->throw_error(
333 sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'},
334 $roles, $methods, $class->name
335 );
336 }
337 }
338
3a63a2e7 339 foreach my $role_spec (@roles) {
340 my($role_name, $args) = @{$role_spec};
21498b08 341
3a63a2e7 342 my $role = $role_name->meta;
21498b08 343
7a50b450 344 $args = $role->_canonicalize_apply_args($class, %{$args});
345
3a63a2e7 346 $role->_check_required_methods($class, $args, @roles);
347 $role->_apply_methods($class, $args);
348 $role->_apply_attributes($class, $args);
349 $role->_apply_modifiers($class, $args);
350 $role->_append_roles($class, $args);
21498b08 351 }
3a63a2e7 352 return;
21498b08 353}
354
6cfa1e5e 355for my $modifier_type (qw/before after around/) {
3a63a2e7 356
357 my $modifier = "${modifier_type}_method_modifiers";
358 my $add_method_modifier = sub {
fc0e0bbd 359 my ($self, $method_name, $method) = @_;
360
3a63a2e7 361 push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
362 return;
fc0e0bbd 363 };
3370794f 364 my $has_method_modifiers = sub{
365 my($self, $method_name) = @_;
366 my $m = $self->{$modifier}->{$method_name};
367 return $m && @{$m} != 0;
368 };
3a63a2e7 369 my $get_method_modifiers = sub {
370 my ($self, $method_name) = @_;
371 return @{ $self->{$modifier}->{$method_name} ||= [] }
c2f128e7 372 };
c2f128e7 373
3a63a2e7 374 no strict 'refs';
375 *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier;
3370794f 376 *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
3a63a2e7 377 *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
378}
47f36c05 379
6cfa1e5e 380sub add_override_method_modifier{
381 my($self, $method_name, $method) = @_;
382
60b5c3be 383 if($self->has_method($method_name)){
384 # This error happens in the override keyword or during role composition,
385 # so I added a message, "A local method of ...", only for compatibility (gfx)
8e64d0fa 386 $self->throw_error("Cannot add an override of method '$method_name' "
60b5c3be 387 . "because there is a local version of '$method_name'"
388 . "(A local method of the same name as been found)");
389 }
6cfa1e5e 390
391 $self->{override_method_modifiers}->{$method_name} = $method;
392}
393
8e64d0fa 394sub has_override_method_modifier {
395 my ($self, $method_name) = @_;
396 return exists $self->{override_method_modifiers}->{$method_name};
397}
398
399sub get_override_method_modifier {
400 my ($self, $method_name) = @_;
401 return $self->{override_method_modifiers}->{$method_name};
6cfa1e5e 402}
403
404sub get_method_modifier_list {
405 my($self, $modifier_type) = @_;
406
407 return keys %{ $self->{$modifier_type . '_method_modifiers'} };
408}
409
67199842 410# This is currently not passing all the Moose tests.
411sub does_role {
412 my ($self, $role_name) = @_;
413
414 (defined $role_name)
fce211ae 415 || $self->throw_error("You must supply a role name to look for");
67199842 416
417 # if we are it,.. then return true
418 return 1 if $role_name eq $self->name;
3a63a2e7 419 # otherwise.. check our children
420 for my $role (@{ $self->get_roles }) {
67199842 421 return 1 if $role->does_role($role_name);
422 }
423 return 0;
424}
425
426
a2227e71 4271;
428
1820fffe 429__END__
430
431=head1 NAME
432
433Mouse::Meta::Role - The Mouse Role metaclass
434
435=head1 SEE ALSO
436
437L<Moose::Meta::Role>
438
439=cut