remove method modifier MOP from Meta::Role
[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
e185c027 228
bdabd620 229## applying a role to a class ...
230
a2eec5e7 231sub _check_excluded_roles {
bdabd620 232 my ($self, $other) = @_;
d79e62fd 233 if ($other->excludes_role($self->name)) {
234 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
235 }
d79e62fd 236 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
9c429218 237 if ($other->does_role($excluded_role_name)) {
d79e62fd 238 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
239 }
240 else {
241 if ($other->isa('Moose::Meta::Role')) {
d79e62fd 242 $other->add_excluded_roles($excluded_role_name);
243 }
a2eec5e7 244 # else -> ignore it :)
d79e62fd 245 }
246 }
a2eec5e7 247}
248
249sub _check_required_methods {
250 my ($self, $other) = @_;
1331430a 251 # NOTE:
252 # we might need to move this down below the
253 # the attributes so that we can require any
254 # attribute accessors. However I am thinking
255 # that maybe those are somehow exempt from
256 # the require methods stuff.
257 foreach my $required_method_name ($self->get_required_method_list) {
8c835eba 258
259 # FIXME:
260 # This should not call has_method, instead it should
261 # call find_method_by_name (to be added to Class::MOP)
fa1be058 262 unless ($other->has_method($required_method_name)) {
263 if ($other->isa('Moose::Meta::Role')) {
264 $other->add_required_methods($required_method_name);
265 }
266 else {
267 confess "'" . $self->name . "' requires the method '$required_method_name' " .
268 "to be implemented by '" . $other->name . "'";
269 }
270 }
38f1204c 271 else {
272 # NOTE:
273 # we need to make sure that the method is
274 # not a method modifier, because those do
275 # not satisfy the requirements ...
8c835eba 276
277 # FIXME:
278 # This should also call find_method_by_name
38f1204c 279 my $method = $other->get_method($required_method_name);
280 # check if it is an override or a generated accessor ..
281 (!$method->isa('Moose::Meta::Method::Overriden') &&
282 !$method->isa('Class::MOP::Attribute::Accessor'))
283 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
284 "to be implemented by '" . $other->name . "', the method is only a method modifier";
285 # before/after/around methods are a little trickier
286 # since we wrap the original local method (if applicable)
287 # so we need to check if the original wrapped method is
288 # from the same package, and not a wrap of the super method
289 if ($method->isa('Class::MOP::Method::Wrapped')) {
290 ($method->get_original_method->package_name eq $other->name)
291 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
292 "to be implemented by '" . $other->name . "', the method is only a method modifier";
293 }
294 }
a2eec5e7 295 }
296}
297
298sub _apply_attributes {
299 my ($self, $other) = @_;
bdabd620 300 foreach my $attribute_name ($self->get_attribute_list) {
db1ab48d 301 # it if it has one already
a2eec5e7 302 if ($other->has_attribute($attribute_name) &&
303 # make sure we haven't seen this one already too
304 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
db1ab48d 305 # see if we are being composed
306 # into a role or not
a2eec5e7 307 if ($other->isa('Moose::Meta::Role')) {
db1ab48d 308 # all attribute conflicts between roles
309 # result in an immediate fatal error
310 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
311 "during composition. This is fatal error and cannot be disambiguated.";
312 }
313 else {
314 # but if this is a class, we
315 # can safely skip adding the
316 # attribute to the class
317 next;
318 }
319 }
320 else {
db1ab48d 321 $other->add_attribute(
322 $attribute_name,
a2eec5e7 323 $self->get_attribute($attribute_name)
db1ab48d 324 );
325 }
a2eec5e7 326 }
327}
328
329sub _apply_methods {
330 my ($self, $other) = @_;
bdabd620 331 foreach my $method_name ($self->get_method_list) {
db1ab48d 332 # it if it has one already
d30bc041 333 if ($other->has_method($method_name) &&
334 # and if they are not the same thing ...
335 $other->get_method($method_name) != $self->get_method($method_name)) {
db1ab48d 336 # see if we are composing into a role
337 if ($other->isa('Moose::Meta::Role')) {
338 # method conflicts between roles result
339 # in the method becoming a requirement
340 $other->add_required_methods($method_name);
341 # NOTE:
342 # we have to remove the method from our
343 # role, if this is being called from combine()
344 # which means the meta is an anon class
345 # this *may* cause problems later, but it
346 # is probably fairly safe to assume that
347 # anon classes will only be used internally
348 # or by people who know what they are doing
349 $other->_role_meta->remove_method($method_name)
350 if $other->_role_meta->name =~ /__ANON__/;
351 }
352 else {
353 next;
354 }
355 }
356 else {
357 # add it, although it could be overriden
358 $other->alias_method(
359 $method_name,
360 $self->get_method($method_name)
361 );
362 }
a2eec5e7 363 }
364}
365
a2eec5e7 366sub apply {
367 my ($self, $other) = @_;
bdabd620 368
a2eec5e7 369 $self->_check_excluded_roles($other);
370 $self->_check_required_methods($other);
371
372 $self->_apply_attributes($other);
373 $self->_apply_methods($other);
d63f8289 374
bdabd620 375 $other->add_role($self);
376}
377
db1ab48d 378sub combine {
379 my ($class, @roles) = @_;
380
381 my $combined = $class->new(
382 ':role_meta' => Moose::Meta::Class->create_anon_class()
383 );
384
385 foreach my $role (@roles) {
386 $role->apply($combined);
387 }
388
d05cd563 389 $combined->_clean_up_required_methods;
db1ab48d 390
391 return $combined;
392}
393
a7d0cd00 394package Moose::Meta::Role::Method;
395
396use strict;
397use warnings;
398
399our $VERSION = '0.01';
400
401use base 'Class::MOP::Method';
e185c027 402
4031;
404
405__END__
406
407=pod
408
409=head1 NAME
410
411Moose::Meta::Role - The Moose Role metaclass
412
413=head1 DESCRIPTION
414
79592a54 415Moose's Roles are being actively developed, please see L<Moose::Role>
02a0fb52 416for more information. For the most part, this has no user-serviceable
417parts inside. It's API is still subject to some change (although
418probably not that much really).
79592a54 419
e185c027 420=head1 METHODS
421
422=over 4
423
424=item B<meta>
425
426=item B<new>
427
78cd1d3b 428=item B<apply>
429
db1ab48d 430=item B<combine>
431
e185c027 432=back
433
434=over 4
435
436=item B<name>
437
438=item B<version>
439
440=item B<role_meta>
441
442=back
443
444=over 4
445
80572233 446=item B<get_roles>
447
448=item B<add_role>
449
450=item B<does_role>
451
452=back
453
454=over 4
455
d79e62fd 456=item B<add_excluded_roles>
457
458=item B<excludes_role>
459
460=item B<get_excluded_roles_list>
461
462=item B<get_excluded_roles_map>
463
2b14ac61 464=item B<calculate_all_roles>
465
d79e62fd 466=back
467
468=over 4
469
e185c027 470=item B<get_method>
471
472=item B<has_method>
473
bdabd620 474=item B<alias_method>
475
e185c027 476=item B<get_method_list>
477
478=back
479
480=over 4
481
482=item B<add_attribute>
483
484=item B<has_attribute>
485
486=item B<get_attribute>
487
488=item B<get_attribute_list>
489
490=item B<get_attribute_map>
491
492=item B<remove_attribute>
493
494=back
495
496=over 4
497
1331430a 498=item B<add_required_methods>
499
38f1204c 500=item B<remove_required_methods>
501
1331430a 502=item B<get_required_method_list>
503
504=item B<get_required_methods_map>
505
506=item B<requires_method>
507
508=back
509
510=over 4
511
80572233 512=item B<add_after_method_modifier>
513
514=item B<add_around_method_modifier>
515
516=item B<add_before_method_modifier>
517
518=item B<add_override_method_modifier>
519
520=over 4
521
522=back
523
524=item B<has_after_method_modifiers>
525
526=item B<has_around_method_modifiers>
527
528=item B<has_before_method_modifiers>
529
530=item B<has_override_method_modifier>
531
532=over 4
533
534=back
535
536=item B<get_after_method_modifiers>
e185c027 537
80572233 538=item B<get_around_method_modifiers>
e185c027 539
80572233 540=item B<get_before_method_modifiers>
e185c027 541
542=item B<get_method_modifier_list>
543
80572233 544=over 4
545
546=back
547
548=item B<get_override_method_modifier>
549
550=item B<get_after_method_modifiers_map>
551
552=item B<get_around_method_modifiers_map>
553
554=item B<get_before_method_modifiers_map>
e185c027 555
80572233 556=item B<get_override_method_modifiers_map>
e185c027 557
558=back
559
560=head1 BUGS
561
562All complex software has bugs lurking in it, and this module is no
563exception. If you find a bug please either email me, or add the bug
564to cpan-RT.
565
566=head1 AUTHOR
567
568Stevan Little E<lt>stevan@iinteractive.comE<gt>
569
570=head1 COPYRIGHT AND LICENSE
571
572Copyright 2006 by Infinity Interactive, Inc.
573
574L<http://www.iinteractive.com>
575
576This library is free software; you can redistribute it and/or modify
577it under the same terms as Perl itself.
578
b8aeb4dc 579=cut