2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
11 use Moose::Meta::Class;
13 our $VERSION = '0.02';
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')) {
271 # all attribute conflicts between roles
272 # result in an immediate fatal error
273 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
274 "during composition. This is fatal error and cannot be disambiguated.";
277 # but if this is a class, we
278 # can safely skip adding the
279 # attribute to the class
284 # add it, although it could be overriden
285 $other->add_attribute(
287 %{$self->get_attribute($attribute_name)}
292 foreach my $method_name ($self->get_method_list) {
293 # it if it has one already
294 if ($other->has_method($method_name)) {
295 # see if we are composing into a role
296 if ($other->isa('Moose::Meta::Role')) {
297 # method conflicts between roles result
298 # in the method becoming a requirement
299 $other->add_required_methods($method_name);
301 # we have to remove the method from our
302 # role, if this is being called from combine()
303 # which means the meta is an anon class
304 # this *may* cause problems later, but it
305 # is probably fairly safe to assume that
306 # anon classes will only be used internally
307 # or by people who know what they are doing
308 $other->_role_meta->remove_method($method_name)
309 if $other->_role_meta->name =~ /__ANON__/;
316 # add it, although it could be overriden
317 $other->alias_method(
319 $self->get_method($method_name)
324 foreach my $method_name ($self->get_method_modifier_list('override')) {
325 # skip it if it has one already
326 next if $other->has_method($method_name);
327 # add it, although it could be overriden
328 $other->add_override_method_modifier(
330 $self->get_override_method_modifier($method_name),
335 foreach my $method_name ($self->get_method_modifier_list('before')) {
336 $other->add_before_method_modifier(
339 ) foreach $self->get_before_method_modifiers($method_name);
342 foreach my $method_name ($self->get_method_modifier_list('after')) {
343 $other->add_after_method_modifier(
346 ) foreach $self->get_after_method_modifiers($method_name);
349 foreach my $method_name ($self->get_method_modifier_list('around')) {
350 $other->add_around_method_modifier(
353 ) foreach $self->get_around_method_modifiers($method_name);
356 $other->add_role($self);
360 my ($class, @roles) = @_;
362 my $combined = $class->new(
363 ':role_meta' => Moose::Meta::Class->create_anon_class()
366 foreach my $role (@roles) {
367 $role->apply($combined);
370 $combined->_clean_up_required_methods;
372 #warn ">>> req-methods: " . (join ", " => $combined->get_required_method_list) . "\n";
373 #warn ">>> methods: " . (join ", " => $combined->get_method_list) . "\n";
374 #warn ">>> attrs: " . (join ", " => $combined->get_attribute_list) . "\n";
379 package Moose::Meta::Role::Method;
384 our $VERSION = '0.01';
386 use base 'Class::MOP::Method';
396 Moose::Meta::Role - The Moose Role metaclass
400 Moose's Roles are being actively developed, please see L<Moose::Role>
401 for more information. For the most part, this has no user-serviceable
402 parts inside. It's API is still subject to some change (although
403 probably not that much really).
445 =item B<alias_method>
447 =item B<get_method_list>
453 =item B<add_attribute>
455 =item B<has_attribute>
457 =item B<get_attribute>
459 =item B<get_attribute_list>
461 =item B<get_attribute_map>
463 =item B<remove_attribute>
469 =item B<add_required_methods>
471 =item B<get_required_method_list>
473 =item B<get_required_methods_map>
475 =item B<requires_method>
481 =item B<add_after_method_modifier>
483 =item B<add_around_method_modifier>
485 =item B<add_before_method_modifier>
487 =item B<add_override_method_modifier>
493 =item B<has_after_method_modifiers>
495 =item B<has_around_method_modifiers>
497 =item B<has_before_method_modifiers>
499 =item B<has_override_method_modifier>
505 =item B<get_after_method_modifiers>
507 =item B<get_around_method_modifiers>
509 =item B<get_before_method_modifiers>
511 =item B<get_method_modifier_list>
517 =item B<get_override_method_modifier>
519 =item B<get_after_method_modifiers_map>
521 =item B<get_around_method_modifiers_map>
523 =item B<get_before_method_modifiers_map>
525 =item B<get_override_method_modifiers_map>
531 All complex software has bugs lurking in it, and this module is no
532 exception. If you find a bug please either email me, or add the bug
537 Stevan Little E<lt>stevan@iinteractive.comE<gt>
539 =head1 COPYRIGHT AND LICENSE
541 Copyright 2006 by Infinity Interactive, Inc.
543 L<http://www.iinteractive.com>
545 This library is free software; you can redistribute it and/or modify
546 it under the same terms as Perl itself.