2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
11 use Moose::Meta::Class;
13 our $VERSION = '0.03';
17 ## the meta for the role package
19 __PACKAGE__->meta->add_attribute('_role_meta' => (
20 reader => '_role_meta',
21 init_arg => ':role_meta'
26 __PACKAGE__->meta->add_attribute('roles' => (
27 reader => 'get_roles',
33 __PACKAGE__->meta->add_attribute('attribute_map' => (
34 reader => 'get_attribute_map',
40 __PACKAGE__->meta->add_attribute('required_methods' => (
41 reader => 'get_required_methods_map',
47 __PACKAGE__->meta->add_attribute('before_method_modifiers' => (
48 reader => 'get_before_method_modifiers_map',
49 default => sub { {} } # (<name> => [ (CODE) ])
52 __PACKAGE__->meta->add_attribute('after_method_modifiers' => (
53 reader => 'get_after_method_modifiers_map',
54 default => sub { {} } # (<name> => [ (CODE) ])
57 __PACKAGE__->meta->add_attribute('around_method_modifiers' => (
58 reader => 'get_around_method_modifiers_map',
59 default => sub { {} } # (<name> => [ (CODE) ])
62 __PACKAGE__->meta->add_attribute('override_method_modifiers' => (
63 reader => 'get_override_method_modifiers_map',
64 default => sub { {} } # (<name> => CODE)
72 $options{':role_meta'} = Moose::Meta::Class->initialize(
74 ':method_metaclass' => 'Moose::Meta::Role::Method'
75 ) unless defined $options{':role_meta'} &&
76 $options{':role_meta'}->isa('Moose::Meta::Class');
77 my $self = $class->meta->new_object(%options);
84 my ($self, $role) = @_;
85 (blessed($role) && $role->isa('Moose::Meta::Role'))
86 || confess "Roles must be instances of Moose::Meta::Role";
87 push @{$self->get_roles} => $role;
91 my ($self, $role_name) = @_;
93 || confess "You must supply a role name to look for";
94 # if we are it,.. then return true
95 return 1 if $role_name eq $self->name;
96 # otherwise.. check our children
97 foreach my $role (@{$self->get_roles}) {
98 return 1 if $role->does_role($role_name);
105 sub add_required_methods {
106 my ($self, @methods) = @_;
107 $self->get_required_methods_map->{$_} = undef foreach @methods;
110 sub get_required_method_list {
112 keys %{$self->get_required_methods_map};
115 sub requires_method {
116 my ($self, $method_name) = @_;
117 exists $self->get_required_methods_map->{$method_name} ? 1 : 0;
120 sub _clean_up_required_methods {
122 foreach my $method ($self->get_required_method_list) {
123 delete $self->get_required_methods_map->{$method}
124 if $self->has_method($method);
131 # we delegate to some role_meta methods for convience here
132 # the Moose::Meta::Role is meant to be a read-only interface
133 # to the underlying role package, if you want to manipulate
134 # that, just use ->role_meta
136 sub name { (shift)->_role_meta->name }
137 sub version { (shift)->_role_meta->version }
139 sub get_method { (shift)->_role_meta->get_method(@_) }
140 sub has_method { (shift)->_role_meta->has_method(@_) }
141 sub alias_method { (shift)->_role_meta->alias_method(@_) }
142 sub get_method_list {
146 # this is a kludge for now,... these functions
147 # should not be showing up in the list at all,
148 # but they do, so we need to switch Moose::Role
149 # and Moose to use Sub::Exporter to prevent this
150 !/^(meta|has|extends|blessed|confess|augment|inner|override|super|before|after|around|with|requires)$/
151 } $self->_role_meta->get_method_list;
154 # ... however the items in statis (attributes & method modifiers)
155 # can be removed and added to through this API
160 my ($self, $name, %attr_desc) = @_;
161 $self->get_attribute_map->{$name} = \%attr_desc;
165 my ($self, $name) = @_;
166 exists $self->get_attribute_map->{$name} ? 1 : 0;
170 my ($self, $name) = @_;
171 $self->get_attribute_map->{$name}
174 sub remove_attribute {
175 my ($self, $name) = @_;
176 delete $self->get_attribute_map->{$name}
179 sub get_attribute_list {
181 keys %{$self->get_attribute_map};
186 # mimic the metaclass API
187 sub add_before_method_modifier { (shift)->_add_method_modifier('before', @_) }
188 sub add_around_method_modifier { (shift)->_add_method_modifier('around', @_) }
189 sub add_after_method_modifier { (shift)->_add_method_modifier('after', @_) }
191 sub _add_method_modifier {
192 my ($self, $modifier_type, $method_name, $method) = @_;
193 my $accessor = "get_${modifier_type}_method_modifiers_map";
194 $self->$accessor->{$method_name} = []
195 unless exists $self->$accessor->{$method_name};
196 push @{$self->$accessor->{$method_name}} => $method;
199 sub add_override_method_modifier {
200 my ($self, $method_name, $method) = @_;
201 $self->get_override_method_modifiers_map->{$method_name} = $method;
204 sub has_before_method_modifiers { (shift)->_has_method_modifiers('before', @_) }
205 sub has_around_method_modifiers { (shift)->_has_method_modifiers('around', @_) }
206 sub has_after_method_modifiers { (shift)->_has_method_modifiers('after', @_) }
208 # override just checks for one,..
209 # but we can still re-use stuff
210 sub has_override_method_modifier { (shift)->_has_method_modifiers('override', @_) }
212 sub _has_method_modifiers {
213 my ($self, $modifier_type, $method_name) = @_;
214 my $accessor = "get_${modifier_type}_method_modifiers_map";
216 # for now we assume that if it exists,..
217 # it has at least one modifier in it
218 (exists $self->$accessor->{$method_name}) ? 1 : 0;
221 sub get_before_method_modifiers { (shift)->_get_method_modifiers('before', @_) }
222 sub get_around_method_modifiers { (shift)->_get_method_modifiers('around', @_) }
223 sub get_after_method_modifiers { (shift)->_get_method_modifiers('after', @_) }
225 sub _get_method_modifiers {
226 my ($self, $modifier_type, $method_name) = @_;
227 my $accessor = "get_${modifier_type}_method_modifiers_map";
228 @{$self->$accessor->{$method_name}};
231 sub get_override_method_modifier {
232 my ($self, $method_name) = @_;
233 $self->get_override_method_modifiers_map->{$method_name};
236 sub get_method_modifier_list {
237 my ($self, $modifier_type) = @_;
238 my $accessor = "get_${modifier_type}_method_modifiers_map";
239 keys %{$self->$accessor};
242 ## applying a role to a class ...
245 my ($self, $other) = @_;
248 # we might need to move this down below the
249 # the attributes so that we can require any
250 # attribute accessors. However I am thinking
251 # that maybe those are somehow exempt from
252 # the require methods stuff.
253 foreach my $required_method_name ($self->get_required_method_list) {
254 unless ($other->has_method($required_method_name)) {
255 if ($other->isa('Moose::Meta::Role')) {
256 $other->add_required_methods($required_method_name);
259 confess "'" . $self->name . "' requires the method '$required_method_name' " .
260 "to be implemented by '" . $other->name . "'";
265 foreach my $attribute_name ($self->get_attribute_list) {
266 # it if it has one already
267 if ($other->has_attribute($attribute_name)) {
268 # see if we are being composed
270 if ($other->isa('Moose::Meta::Role')) {
273 # it is possible for these attributes
274 # to actually both be from the same
275 # origin (some common ancestor role)
276 # so we need to find a way to check this
278 # all attribute conflicts between roles
279 # result in an immediate fatal error
280 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
281 "during composition. This is fatal error and cannot be disambiguated.";
284 # but if this is a class, we
285 # can safely skip adding the
286 # attribute to the class
291 # add it, although it could be overriden
292 $other->add_attribute(
294 %{$self->get_attribute($attribute_name)}
299 foreach my $method_name ($self->get_method_list) {
300 # it if it has one already
301 if ($other->has_method($method_name)) {
302 # see if we are composing into a role
303 if ($other->isa('Moose::Meta::Role')) {
304 # method conflicts between roles result
305 # in the method becoming a requirement
306 $other->add_required_methods($method_name);
308 # we have to remove the method from our
309 # role, if this is being called from combine()
310 # which means the meta is an anon class
311 # this *may* cause problems later, but it
312 # is probably fairly safe to assume that
313 # anon classes will only be used internally
314 # or by people who know what they are doing
315 $other->_role_meta->remove_method($method_name)
316 if $other->_role_meta->name =~ /__ANON__/;
323 # add it, although it could be overriden
324 $other->alias_method(
326 $self->get_method($method_name)
331 foreach my $method_name ($self->get_method_modifier_list('override')) {
332 # it if it has one already then ...
333 if ($other->has_method($method_name)) {
334 # if it is being composed into another role
335 # we have a conflict here, because you cannot
336 # combine an overriden method with a locally
338 if ($other->isa('Moose::Meta::Role')) {
339 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
340 "during composition (A local method of the same name as been found). This " .
344 # if it is a class, then we
345 # just ignore this here ...
350 # if no local method is found, then we
351 # must check if we are a role or class
352 if ($other->isa('Moose::Meta::Role')) {
353 # if we are a role, we need to make sure
354 # we dont have a conflict with the role
355 # we are composing into
356 if ($other->has_override_method_modifier($method_name)) {
357 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
358 "during composition (Two 'override' methods of the same name encountered). " .
359 "This is fatal error.";
362 $other->add_override_method_modifier(
364 $self->get_override_method_modifier($method_name),
370 # if it is a class, we just add it
371 $other->add_override_method_modifier(
373 $self->get_override_method_modifier($method_name),
380 foreach my $method_name ($self->get_method_modifier_list('before')) {
381 $other->add_before_method_modifier(
384 ) foreach $self->get_before_method_modifiers($method_name);
387 foreach my $method_name ($self->get_method_modifier_list('after')) {
388 $other->add_after_method_modifier(
391 ) foreach $self->get_after_method_modifiers($method_name);
394 foreach my $method_name ($self->get_method_modifier_list('around')) {
395 $other->add_around_method_modifier(
398 ) foreach $self->get_around_method_modifiers($method_name);
401 $other->add_role($self);
405 my ($class, @roles) = @_;
407 my $combined = $class->new(
408 ':role_meta' => Moose::Meta::Class->create_anon_class()
411 foreach my $role (@roles) {
412 $role->apply($combined);
415 $combined->_clean_up_required_methods;
420 package Moose::Meta::Role::Method;
425 our $VERSION = '0.01';
427 use base 'Class::MOP::Method';
437 Moose::Meta::Role - The Moose Role metaclass
441 Moose's Roles are being actively developed, please see L<Moose::Role>
442 for more information. For the most part, this has no user-serviceable
443 parts inside. It's API is still subject to some change (although
444 probably not that much really).
486 =item B<alias_method>
488 =item B<get_method_list>
494 =item B<add_attribute>
496 =item B<has_attribute>
498 =item B<get_attribute>
500 =item B<get_attribute_list>
502 =item B<get_attribute_map>
504 =item B<remove_attribute>
510 =item B<add_required_methods>
512 =item B<get_required_method_list>
514 =item B<get_required_methods_map>
516 =item B<requires_method>
522 =item B<add_after_method_modifier>
524 =item B<add_around_method_modifier>
526 =item B<add_before_method_modifier>
528 =item B<add_override_method_modifier>
534 =item B<has_after_method_modifiers>
536 =item B<has_around_method_modifiers>
538 =item B<has_before_method_modifiers>
540 =item B<has_override_method_modifier>
546 =item B<get_after_method_modifiers>
548 =item B<get_around_method_modifiers>
550 =item B<get_before_method_modifiers>
552 =item B<get_method_modifier_list>
558 =item B<get_override_method_modifier>
560 =item B<get_after_method_modifiers_map>
562 =item B<get_around_method_modifiers_map>
564 =item B<get_before_method_modifiers_map>
566 =item B<get_override_method_modifiers_map>
572 All complex software has bugs lurking in it, and this module is no
573 exception. If you find a bug please either email me, or add the bug
578 Stevan Little E<lt>stevan@iinteractive.comE<gt>
580 =head1 COPYRIGHT AND LICENSE
582 Copyright 2006 by Infinity Interactive, Inc.
584 L<http://www.iinteractive.com>
586 This library is free software; you can redistribute it and/or modify
587 it under the same terms as Perl itself.