2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
10 use B 'svref_2object';
12 use Moose::Meta::Class;
14 our $VERSION = '0.04';
18 ## the meta for the role package
20 __PACKAGE__->meta->add_attribute('_role_meta' => (
21 reader => '_role_meta',
22 init_arg => ':role_meta'
27 __PACKAGE__->meta->add_attribute('roles' => (
28 reader => 'get_roles',
34 __PACKAGE__->meta->add_attribute('excluded_roles_map' => (
35 reader => 'get_excluded_roles_map',
41 __PACKAGE__->meta->add_attribute('attribute_map' => (
42 reader => 'get_attribute_map',
48 __PACKAGE__->meta->add_attribute('required_methods' => (
49 reader => 'get_required_methods_map',
58 $options{':role_meta'} = Moose::Meta::Class->initialize(
60 ':method_metaclass' => 'Moose::Meta::Role::Method'
61 ) unless defined $options{':role_meta'} &&
62 $options{':role_meta'}->isa('Moose::Meta::Class');
63 my $self = $class->meta->new_object(%options);
70 my ($self, $role) = @_;
71 (blessed($role) && $role->isa('Moose::Meta::Role'))
72 || confess "Roles must be instances of Moose::Meta::Role";
73 push @{$self->get_roles} => $role;
76 sub calculate_all_roles {
79 grep { !$seen{$_->name}++ } $self, map { $_->calculate_all_roles } @{ $self->get_roles };
83 my ($self, $role_name) = @_;
85 || confess "You must supply a role name to look for";
86 # if we are it,.. then return true
87 return 1 if $role_name eq $self->name;
88 # otherwise.. check our children
89 foreach my $role (@{$self->get_roles}) {
90 return 1 if $role->does_role($role_name);
97 sub add_excluded_roles {
98 my ($self, @excluded_role_names) = @_;
99 $self->get_excluded_roles_map->{$_} = undef foreach @excluded_role_names;
102 sub get_excluded_roles_list {
104 keys %{$self->get_excluded_roles_map};
108 my ($self, $role_name) = @_;
109 exists $self->get_excluded_roles_map->{$role_name} ? 1 : 0;
114 sub add_required_methods {
115 my ($self, @methods) = @_;
116 $self->get_required_methods_map->{$_} = undef foreach @methods;
119 sub remove_required_methods {
120 my ($self, @methods) = @_;
121 delete $self->get_required_methods_map->{$_} foreach @methods;
124 sub get_required_method_list {
126 keys %{$self->get_required_methods_map};
129 sub requires_method {
130 my ($self, $method_name) = @_;
131 exists $self->get_required_methods_map->{$method_name} ? 1 : 0;
134 sub _clean_up_required_methods {
136 foreach my $method ($self->get_required_method_list) {
137 $self->remove_required_methods($method)
138 if $self->has_method($method);
145 # we delegate to some role_meta methods for convience here
146 # the Moose::Meta::Role is meant to be a read-only interface
147 # to the underlying role package, if you want to manipulate
148 # that, just use ->role_meta
150 sub name { (shift)->_role_meta->name }
151 sub version { (shift)->_role_meta->version }
153 sub get_method { (shift)->_role_meta->get_method(@_) }
154 sub find_method_by_name { (shift)->_role_meta->find_method_by_name(@_) }
155 sub has_method { (shift)->_role_meta->has_method(@_) }
156 sub alias_method { (shift)->_role_meta->alias_method(@_) }
157 sub get_method_list {
161 # this is a kludge for now,... these functions
162 # should not be showing up in the list at all,
163 # but they do, so we need to switch Moose::Role
164 # and Moose to use Sub::Exporter to prevent this
165 !/^(meta|has|extends|blessed|confess|augment|inner|override|super|before|after|around|with|requires)$/
166 } $self->_role_meta->get_method_list;
169 # ... however the items in statis (attributes & method modifiers)
170 # can be removed and added to through this API
178 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
184 $self->get_attribute_map->{$name} = $attr_desc;
188 my ($self, $name) = @_;
189 exists $self->get_attribute_map->{$name} ? 1 : 0;
193 my ($self, $name) = @_;
194 $self->get_attribute_map->{$name}
197 sub remove_attribute {
198 my ($self, $name) = @_;
199 delete $self->get_attribute_map->{$name}
202 sub get_attribute_list {
204 keys %{$self->get_attribute_map};
208 ## applying a role to a class ...
210 sub _check_excluded_roles {
211 my ($self, $other) = @_;
212 if ($other->excludes_role($self->name)) {
213 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
215 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
216 if ($other->does_role($excluded_role_name)) {
217 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
220 if ($other->isa('Moose::Meta::Role')) {
221 $other->add_excluded_roles($excluded_role_name);
223 # else -> ignore it :)
228 sub _check_required_methods {
229 my ($self, $other) = @_;
231 # we might need to move this down below the
232 # the attributes so that we can require any
233 # attribute accessors. However I am thinking
234 # that maybe those are somehow exempt from
235 # the require methods stuff.
236 foreach my $required_method_name ($self->get_required_method_list) {
238 unless ($other->find_method_by_name($required_method_name)) {
239 if ($other->isa('Moose::Meta::Role')) {
240 $other->add_required_methods($required_method_name);
243 confess "'" . $self->name . "' requires the method '$required_method_name' " .
244 "to be implemented by '" . $other->name . "'";
250 sub _apply_attributes {
251 my ($self, $other) = @_;
252 foreach my $attribute_name ($self->get_attribute_list) {
253 # it if it has one already
254 if ($other->has_attribute($attribute_name) &&
255 # make sure we haven't seen this one already too
256 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
257 # see if we are being composed
259 if ($other->isa('Moose::Meta::Role')) {
260 # all attribute conflicts between roles
261 # result in an immediate fatal error
262 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
263 "during composition. This is fatal error and cannot be disambiguated.";
266 # but if this is a class, we
267 # can safely skip adding the
268 # attribute to the class
273 $other->add_attribute(
275 $self->get_attribute($attribute_name)
282 my ($self, $other) = @_;
283 foreach my $method_name ($self->get_method_list) {
284 # it if it has one already
285 if ($other->has_method($method_name) &&
286 # and if they are not the same thing ...
287 $other->get_method($method_name) != $self->get_method($method_name)) {
288 # see if we are composing into a role
289 if ($other->isa('Moose::Meta::Role')) {
290 # method conflicts between roles result
291 # in the method becoming a requirement
292 $other->add_required_methods($method_name);
294 # we have to remove the method from our
295 # role, if this is being called from combine()
296 # which means the meta is an anon class
297 # this *may* cause problems later, but it
298 # is probably fairly safe to assume that
299 # anon classes will only be used internally
300 # or by people who know what they are doing
301 $other->_role_meta->remove_method($method_name)
302 if $other->_role_meta->name =~ /__ANON__/;
309 # add it, although it could be overriden
310 $other->alias_method(
312 $self->get_method($method_name)
319 my ($self, $other) = @_;
321 ($other->isa('Moose::Meta::Class') || $other->isa('Moose::Meta::Role'))
322 || confess "You must apply a role to a metaclass, not ($other)";
324 $self->_check_excluded_roles($other);
325 $self->_check_required_methods($other);
327 $self->_apply_attributes($other);
328 $self->_apply_methods($other);
330 $other->add_role($self);
334 my ($class, @roles) = @_;
336 my $combined = $class->new(
337 ':role_meta' => Moose::Meta::Class->create_anon_class()
340 foreach my $role (@roles) {
341 $role->apply($combined);
344 $combined->_clean_up_required_methods;
349 package Moose::Meta::Role::Method;
354 our $VERSION = '0.01';
356 use base 'Class::MOP::Method';
366 Moose::Meta::Role - The Moose Role metaclass
370 Moose's Roles are being actively developed, please see L<Moose::Role>
371 for more information. For the most part, this has no user-serviceable
372 parts inside. It's API is still subject to some change (although
373 probably not that much really).
411 =item B<add_excluded_roles>
413 =item B<excludes_role>
415 =item B<get_excluded_roles_list>
417 =item B<get_excluded_roles_map>
419 =item B<calculate_all_roles>
425 =item B<find_method_by_name>
431 =item B<alias_method>
433 =item B<get_method_list>
439 =item B<add_attribute>
441 =item B<has_attribute>
443 =item B<get_attribute>
445 =item B<get_attribute_list>
447 =item B<get_attribute_map>
449 =item B<remove_attribute>
455 =item B<add_required_methods>
457 =item B<remove_required_methods>
459 =item B<get_required_method_list>
461 =item B<get_required_methods_map>
463 =item B<requires_method>
469 All complex software has bugs lurking in it, and this module is no
470 exception. If you find a bug please either email me, or add the bug
475 Stevan Little E<lt>stevan@iinteractive.comE<gt>
477 =head1 COPYRIGHT AND LICENSE
479 Copyright 2006 by Infinity Interactive, Inc.
481 L<http://www.iinteractive.com>
483 This library is free software; you can redistribute it and/or modify
484 it under the same terms as Perl itself.