clean up the tests a bit
[gitmo/Moose.git] / lib / Moose / Meta / Role.pm
CommitLineData
e185c027 1
2package Moose::Meta::Role;
3
4use strict;
5use warnings;
6use metaclass;
7
bdabd620 8use Carp 'confess';
9use Scalar::Util 'blessed';
d30bc041 10use B 'svref_2object';
bdabd620 11
12use Moose::Meta::Class;
e185c027 13
e39d707f 14our $VERSION = '0.04';
e185c027 15
bdabd620 16## Attributes
17
80572233 18## the meta for the role package
19
bdabd620 20__PACKAGE__->meta->add_attribute('_role_meta' => (
21 reader => '_role_meta',
22 init_arg => ':role_meta'
80572233 23));
24
25## roles
26
27__PACKAGE__->meta->add_attribute('roles' => (
28 reader => 'get_roles',
29 default => sub { [] }
e185c027 30));
31
d79e62fd 32## excluded roles
33
34__PACKAGE__->meta->add_attribute('excluded_roles_map' => (
35 reader => 'get_excluded_roles_map',
36 default => sub { {} }
37));
38
80572233 39## attributes
40
e185c027 41__PACKAGE__->meta->add_attribute('attribute_map' => (
42 reader => 'get_attribute_map',
43 default => sub { {} }
44));
45
1331430a 46## required methods
47
48__PACKAGE__->meta->add_attribute('required_methods' => (
49 reader => 'get_required_methods_map',
50 default => sub { {} }
51));
52
80572233 53## method modifiers
54
55__PACKAGE__->meta->add_attribute('before_method_modifiers' => (
56 reader => 'get_before_method_modifiers_map',
bdabd620 57 default => sub { {} } # (<name> => [ (CODE) ])
e185c027 58));
59
80572233 60__PACKAGE__->meta->add_attribute('after_method_modifiers' => (
61 reader => 'get_after_method_modifiers_map',
bdabd620 62 default => sub { {} } # (<name> => [ (CODE) ])
80572233 63));
64
65__PACKAGE__->meta->add_attribute('around_method_modifiers' => (
66 reader => 'get_around_method_modifiers_map',
bdabd620 67 default => sub { {} } # (<name> => [ (CODE) ])
80572233 68));
69
70__PACKAGE__->meta->add_attribute('override_method_modifiers' => (
71 reader => 'get_override_method_modifiers_map',
72 default => sub { {} } # (<name> => CODE)
73));
74
bdabd620 75## Methods
80572233 76
e185c027 77sub new {
78 my $class = shift;
79 my %options = @_;
bdabd620 80 $options{':role_meta'} = Moose::Meta::Class->initialize(
a7d0cd00 81 $options{role_name},
82 ':method_metaclass' => 'Moose::Meta::Role::Method'
db1ab48d 83 ) unless defined $options{':role_meta'} &&
84 $options{':role_meta'}->isa('Moose::Meta::Class');
e185c027 85 my $self = $class->meta->new_object(%options);
86 return $self;
87}
88
80572233 89## subroles
90
91sub add_role {
92 my ($self, $role) = @_;
93 (blessed($role) && $role->isa('Moose::Meta::Role'))
94 || confess "Roles must be instances of Moose::Meta::Role";
95 push @{$self->get_roles} => $role;
96}
97
b8aeb4dc 98sub calculate_all_roles {
99 my $self = shift;
100 my %seen;
101 grep { !$seen{$_->name}++ } $self, map { $_->calculate_all_roles } @{ $self->get_roles };
102}
103
80572233 104sub does_role {
105 my ($self, $role_name) = @_;
106 (defined $role_name)
107 || confess "You must supply a role name to look for";
bdabd620 108 # if we are it,.. then return true
109 return 1 if $role_name eq $self->name;
110 # otherwise.. check our children
80572233 111 foreach my $role (@{$self->get_roles}) {
bdabd620 112 return 1 if $role->does_role($role_name);
80572233 113 }
114 return 0;
115}
116
d79e62fd 117## excluded roles
118
119sub add_excluded_roles {
120 my ($self, @excluded_role_names) = @_;
121 $self->get_excluded_roles_map->{$_} = undef foreach @excluded_role_names;
122}
123
124sub get_excluded_roles_list {
125 my ($self) = @_;
126 keys %{$self->get_excluded_roles_map};
127}
128
129sub excludes_role {
130 my ($self, $role_name) = @_;
131 exists $self->get_excluded_roles_map->{$role_name} ? 1 : 0;
132}
133
1331430a 134## required methods
135
136sub add_required_methods {
137 my ($self, @methods) = @_;
138 $self->get_required_methods_map->{$_} = undef foreach @methods;
139}
140
38f1204c 141sub remove_required_methods {
142 my ($self, @methods) = @_;
143 delete $self->get_required_methods_map->{$_} foreach @methods;
144}
145
1331430a 146sub get_required_method_list {
147 my ($self) = @_;
148 keys %{$self->get_required_methods_map};
149}
150
151sub requires_method {
152 my ($self, $method_name) = @_;
153 exists $self->get_required_methods_map->{$method_name} ? 1 : 0;
154}
155
db1ab48d 156sub _clean_up_required_methods {
157 my $self = shift;
158 foreach my $method ($self->get_required_method_list) {
38f1204c 159 $self->remove_required_methods($method)
db1ab48d 160 if $self->has_method($method);
161 }
162}
163
80572233 164## methods
165
e185c027 166# NOTE:
167# we delegate to some role_meta methods for convience here
168# the Moose::Meta::Role is meant to be a read-only interface
169# to the underlying role package, if you want to manipulate
170# that, just use ->role_meta
171
bdabd620 172sub name { (shift)->_role_meta->name }
173sub version { (shift)->_role_meta->version }
e185c027 174
bdabd620 175sub get_method { (shift)->_role_meta->get_method(@_) }
176sub has_method { (shift)->_role_meta->has_method(@_) }
177sub alias_method { (shift)->_role_meta->alias_method(@_) }
e185c027 178sub get_method_list {
179 my ($self) = @_;
bdabd620 180 grep {
181 # NOTE:
182 # this is a kludge for now,... these functions
183 # should not be showing up in the list at all,
184 # but they do, so we need to switch Moose::Role
185 # and Moose to use Sub::Exporter to prevent this
1331430a 186 !/^(meta|has|extends|blessed|confess|augment|inner|override|super|before|after|around|with|requires)$/
bdabd620 187 } $self->_role_meta->get_method_list;
e185c027 188}
189
190# ... however the items in statis (attributes & method modifiers)
191# can be removed and added to through this API
192
193# attributes
194
195sub add_attribute {
a2eec5e7 196 my $self = shift;
197 my $name = shift;
198 my $attr_desc;
199 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
200 $attr_desc = $_[0];
201 }
202 else {
203 $attr_desc = { @_ };
204 }
205 $self->get_attribute_map->{$name} = $attr_desc;
e185c027 206}
207
208sub has_attribute {
209 my ($self, $name) = @_;
210 exists $self->get_attribute_map->{$name} ? 1 : 0;
211}
212
213sub get_attribute {
214 my ($self, $name) = @_;
215 $self->get_attribute_map->{$name}
216}
217
218sub remove_attribute {
219 my ($self, $name) = @_;
220 delete $self->get_attribute_map->{$name}
221}
222
223sub get_attribute_list {
224 my ($self) = @_;
225 keys %{$self->get_attribute_map};
226}
227
228# method modifiers
229
80572233 230# mimic the metaclass API
231sub add_before_method_modifier { (shift)->_add_method_modifier('before', @_) }
232sub add_around_method_modifier { (shift)->_add_method_modifier('around', @_) }
233sub add_after_method_modifier { (shift)->_add_method_modifier('after', @_) }
234
235sub _add_method_modifier {
e185c027 236 my ($self, $modifier_type, $method_name, $method) = @_;
80572233 237 my $accessor = "get_${modifier_type}_method_modifiers_map";
238 $self->$accessor->{$method_name} = []
239 unless exists $self->$accessor->{$method_name};
d30bc041 240 my $modifiers = $self->$accessor->{$method_name};
241 # NOTE:
242 # check to see that we aren't adding the
243 # same code twice. We err in favor of the
244 # first on here, this may not be as expected
245 foreach my $modifier (@{$modifiers}) {
246 return if $modifier == $method;
247 }
248 push @{$modifiers} => $method;
e185c027 249}
250
80572233 251sub add_override_method_modifier {
252 my ($self, $method_name, $method) = @_;
e39d707f 253 (!$self->has_method($method_name))
254 || confess "Cannot add an override of method '$method_name' " .
255 "because there is a local version of '$method_name'";
80572233 256 $self->get_override_method_modifiers_map->{$method_name} = $method;
e185c027 257}
258
80572233 259sub has_before_method_modifiers { (shift)->_has_method_modifiers('before', @_) }
260sub has_around_method_modifiers { (shift)->_has_method_modifiers('around', @_) }
261sub has_after_method_modifiers { (shift)->_has_method_modifiers('after', @_) }
262
263# override just checks for one,..
264# but we can still re-use stuff
265sub has_override_method_modifier { (shift)->_has_method_modifiers('override', @_) }
266
267sub _has_method_modifiers {
e185c027 268 my ($self, $modifier_type, $method_name) = @_;
80572233 269 my $accessor = "get_${modifier_type}_method_modifiers_map";
270 # NOTE:
271 # for now we assume that if it exists,..
272 # it has at least one modifier in it
273 (exists $self->$accessor->{$method_name}) ? 1 : 0;
e185c027 274}
275
80572233 276sub get_before_method_modifiers { (shift)->_get_method_modifiers('before', @_) }
277sub get_around_method_modifiers { (shift)->_get_method_modifiers('around', @_) }
278sub get_after_method_modifiers { (shift)->_get_method_modifiers('after', @_) }
279
280sub _get_method_modifiers {
e185c027 281 my ($self, $modifier_type, $method_name) = @_;
80572233 282 my $accessor = "get_${modifier_type}_method_modifiers_map";
283 @{$self->$accessor->{$method_name}};
284}
285
286sub get_override_method_modifier {
287 my ($self, $method_name) = @_;
288 $self->get_override_method_modifiers_map->{$method_name};
e185c027 289}
290
291sub get_method_modifier_list {
292 my ($self, $modifier_type) = @_;
80572233 293 my $accessor = "get_${modifier_type}_method_modifiers_map";
294 keys %{$self->$accessor};
e185c027 295}
296
bdabd620 297## applying a role to a class ...
298
a2eec5e7 299sub _check_excluded_roles {
bdabd620 300 my ($self, $other) = @_;
d79e62fd 301 if ($other->excludes_role($self->name)) {
302 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
303 }
d79e62fd 304 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
9c429218 305 if ($other->does_role($excluded_role_name)) {
d79e62fd 306 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
307 }
308 else {
309 if ($other->isa('Moose::Meta::Role')) {
d79e62fd 310 $other->add_excluded_roles($excluded_role_name);
311 }
a2eec5e7 312 # else -> ignore it :)
d79e62fd 313 }
314 }
a2eec5e7 315}
316
317sub _check_required_methods {
318 my ($self, $other) = @_;
1331430a 319 # NOTE:
320 # we might need to move this down below the
321 # the attributes so that we can require any
322 # attribute accessors. However I am thinking
323 # that maybe those are somehow exempt from
324 # the require methods stuff.
325 foreach my $required_method_name ($self->get_required_method_list) {
8c835eba 326
327 # FIXME:
328 # This should not call has_method, instead it should
329 # call find_method_by_name (to be added to Class::MOP)
fa1be058 330 unless ($other->has_method($required_method_name)) {
331 if ($other->isa('Moose::Meta::Role')) {
332 $other->add_required_methods($required_method_name);
333 }
334 else {
335 confess "'" . $self->name . "' requires the method '$required_method_name' " .
336 "to be implemented by '" . $other->name . "'";
337 }
338 }
38f1204c 339 else {
340 # NOTE:
341 # we need to make sure that the method is
342 # not a method modifier, because those do
343 # not satisfy the requirements ...
8c835eba 344
345 # FIXME:
346 # This should also call find_method_by_name
38f1204c 347 my $method = $other->get_method($required_method_name);
348 # check if it is an override or a generated accessor ..
349 (!$method->isa('Moose::Meta::Method::Overriden') &&
350 !$method->isa('Class::MOP::Attribute::Accessor'))
351 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
352 "to be implemented by '" . $other->name . "', the method is only a method modifier";
353 # before/after/around methods are a little trickier
354 # since we wrap the original local method (if applicable)
355 # so we need to check if the original wrapped method is
356 # from the same package, and not a wrap of the super method
357 if ($method->isa('Class::MOP::Method::Wrapped')) {
358 ($method->get_original_method->package_name eq $other->name)
359 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
360 "to be implemented by '" . $other->name . "', the method is only a method modifier";
361 }
362 }
a2eec5e7 363 }
364}
365
366sub _apply_attributes {
367 my ($self, $other) = @_;
bdabd620 368 foreach my $attribute_name ($self->get_attribute_list) {
db1ab48d 369 # it if it has one already
a2eec5e7 370 if ($other->has_attribute($attribute_name) &&
371 # make sure we haven't seen this one already too
372 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
db1ab48d 373 # see if we are being composed
374 # into a role or not
a2eec5e7 375 if ($other->isa('Moose::Meta::Role')) {
db1ab48d 376 # all attribute conflicts between roles
377 # result in an immediate fatal error
378 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
379 "during composition. This is fatal error and cannot be disambiguated.";
380 }
381 else {
382 # but if this is a class, we
383 # can safely skip adding the
384 # attribute to the class
385 next;
386 }
387 }
388 else {
db1ab48d 389 $other->add_attribute(
390 $attribute_name,
a2eec5e7 391 $self->get_attribute($attribute_name)
db1ab48d 392 );
393 }
a2eec5e7 394 }
395}
396
397sub _apply_methods {
398 my ($self, $other) = @_;
bdabd620 399 foreach my $method_name ($self->get_method_list) {
db1ab48d 400 # it if it has one already
d30bc041 401 if ($other->has_method($method_name) &&
402 # and if they are not the same thing ...
403 $other->get_method($method_name) != $self->get_method($method_name)) {
db1ab48d 404 # see if we are composing into a role
405 if ($other->isa('Moose::Meta::Role')) {
406 # method conflicts between roles result
407 # in the method becoming a requirement
408 $other->add_required_methods($method_name);
409 # NOTE:
410 # we have to remove the method from our
411 # role, if this is being called from combine()
412 # which means the meta is an anon class
413 # this *may* cause problems later, but it
414 # is probably fairly safe to assume that
415 # anon classes will only be used internally
416 # or by people who know what they are doing
417 $other->_role_meta->remove_method($method_name)
418 if $other->_role_meta->name =~ /__ANON__/;
419 }
420 else {
421 next;
422 }
423 }
424 else {
425 # add it, although it could be overriden
426 $other->alias_method(
427 $method_name,
428 $self->get_method($method_name)
429 );
430 }
a2eec5e7 431 }
432}
433
434sub _apply_override_method_modifiers {
435 my ($self, $other) = @_;
bdabd620 436 foreach my $method_name ($self->get_method_modifier_list('override')) {
d05cd563 437 # it if it has one already then ...
438 if ($other->has_method($method_name)) {
439 # if it is being composed into another role
440 # we have a conflict here, because you cannot
441 # combine an overriden method with a locally
442 # defined one
443 if ($other->isa('Moose::Meta::Role')) {
444 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
445 "during composition (A local method of the same name as been found). This " .
446 "is fatal error.";
447 }
448 else {
449 # if it is a class, then we
450 # just ignore this here ...
451 next;
452 }
453 }
454 else {
455 # if no local method is found, then we
456 # must check if we are a role or class
457 if ($other->isa('Moose::Meta::Role')) {
458 # if we are a role, we need to make sure
459 # we dont have a conflict with the role
460 # we are composing into
d30bc041 461 if ($other->has_override_method_modifier($method_name) &&
462 $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
d05cd563 463 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
464 "during composition (Two 'override' methods of the same name encountered). " .
465 "This is fatal error.";
466 }
d30bc041 467 else {
468 # if there is no conflict,
469 # just add it to the role
d05cd563 470 $other->add_override_method_modifier(
d30bc041 471 $method_name,
472 $self->get_override_method_modifier($method_name)
d05cd563 473 );
474 }
475 }
476 else {
d30bc041 477 # if this is not a role, then we need to
478 # find the original package of the method
479 # so that we can tell the class were to
480 # find the right super() method
481 my $method = $self->get_override_method_modifier($method_name);
482 my $package = svref_2object($method)->GV->STASH->NAME;
d05cd563 483 # if it is a class, we just add it
d30bc041 484 $other->add_override_method_modifier($method_name, $method, $package);
d05cd563 485 }
486 }
bdabd620 487 }
a2eec5e7 488}
489
490sub _apply_method_modifiers {
491 my ($self, $modifier_type, $other) = @_;
492 my $add = "add_${modifier_type}_method_modifier";
493 my $get = "get_${modifier_type}_method_modifiers";
494 foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
495 $other->$add(
bdabd620 496 $method_name,
497 $_
a2eec5e7 498 ) foreach $self->$get($method_name);
bdabd620 499 }
a2eec5e7 500}
501
502sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
503sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
504sub _apply_after_method_modifiers { (shift)->_apply_method_modifiers('after' => @_) }
505
506sub apply {
507 my ($self, $other) = @_;
bdabd620 508
a2eec5e7 509 $self->_check_excluded_roles($other);
510 $self->_check_required_methods($other);
511
512 $self->_apply_attributes($other);
513 $self->_apply_methods($other);
514
515 $self->_apply_override_method_modifiers($other);
516 $self->_apply_before_method_modifiers($other);
517 $self->_apply_around_method_modifiers($other);
518 $self->_apply_after_method_modifiers($other);
bdabd620 519
bdabd620 520 $other->add_role($self);
521}
522
db1ab48d 523sub combine {
524 my ($class, @roles) = @_;
525
526 my $combined = $class->new(
527 ':role_meta' => Moose::Meta::Class->create_anon_class()
528 );
529
530 foreach my $role (@roles) {
531 $role->apply($combined);
532 }
533
d05cd563 534 $combined->_clean_up_required_methods;
db1ab48d 535
536 return $combined;
537}
538
a7d0cd00 539package Moose::Meta::Role::Method;
540
541use strict;
542use warnings;
543
544our $VERSION = '0.01';
545
546use base 'Class::MOP::Method';
e185c027 547
5481;
549
550__END__
551
552=pod
553
554=head1 NAME
555
556Moose::Meta::Role - The Moose Role metaclass
557
558=head1 DESCRIPTION
559
79592a54 560Moose's Roles are being actively developed, please see L<Moose::Role>
02a0fb52 561for more information. For the most part, this has no user-serviceable
562parts inside. It's API is still subject to some change (although
563probably not that much really).
79592a54 564
e185c027 565=head1 METHODS
566
567=over 4
568
569=item B<meta>
570
571=item B<new>
572
78cd1d3b 573=item B<apply>
574
db1ab48d 575=item B<combine>
576
e185c027 577=back
578
579=over 4
580
581=item B<name>
582
583=item B<version>
584
585=item B<role_meta>
586
587=back
588
589=over 4
590
80572233 591=item B<get_roles>
592
593=item B<add_role>
594
595=item B<does_role>
596
597=back
598
599=over 4
600
d79e62fd 601=item B<add_excluded_roles>
602
603=item B<excludes_role>
604
605=item B<get_excluded_roles_list>
606
607=item B<get_excluded_roles_map>
608
2b14ac61 609=item B<calculate_all_roles>
610
d79e62fd 611=back
612
613=over 4
614
e185c027 615=item B<get_method>
616
617=item B<has_method>
618
bdabd620 619=item B<alias_method>
620
e185c027 621=item B<get_method_list>
622
623=back
624
625=over 4
626
627=item B<add_attribute>
628
629=item B<has_attribute>
630
631=item B<get_attribute>
632
633=item B<get_attribute_list>
634
635=item B<get_attribute_map>
636
637=item B<remove_attribute>
638
639=back
640
641=over 4
642
1331430a 643=item B<add_required_methods>
644
38f1204c 645=item B<remove_required_methods>
646
1331430a 647=item B<get_required_method_list>
648
649=item B<get_required_methods_map>
650
651=item B<requires_method>
652
653=back
654
655=over 4
656
80572233 657=item B<add_after_method_modifier>
658
659=item B<add_around_method_modifier>
660
661=item B<add_before_method_modifier>
662
663=item B<add_override_method_modifier>
664
665=over 4
666
667=back
668
669=item B<has_after_method_modifiers>
670
671=item B<has_around_method_modifiers>
672
673=item B<has_before_method_modifiers>
674
675=item B<has_override_method_modifier>
676
677=over 4
678
679=back
680
681=item B<get_after_method_modifiers>
e185c027 682
80572233 683=item B<get_around_method_modifiers>
e185c027 684
80572233 685=item B<get_before_method_modifiers>
e185c027 686
687=item B<get_method_modifier_list>
688
80572233 689=over 4
690
691=back
692
693=item B<get_override_method_modifier>
694
695=item B<get_after_method_modifiers_map>
696
697=item B<get_around_method_modifiers_map>
698
699=item B<get_before_method_modifiers_map>
e185c027 700
80572233 701=item B<get_override_method_modifiers_map>
e185c027 702
703=back
704
705=head1 BUGS
706
707All complex software has bugs lurking in it, and this module is no
708exception. If you find a bug please either email me, or add the bug
709to cpan-RT.
710
711=head1 AUTHOR
712
713Stevan Little E<lt>stevan@iinteractive.comE<gt>
714
715=head1 COPYRIGHT AND LICENSE
716
717Copyright 2006 by Infinity Interactive, Inc.
718
719L<http://www.iinteractive.com>
720
721This library is free software; you can redistribute it and/or modify
722it under the same terms as Perl itself.
723
b8aeb4dc 724=cut