2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed', 'reftype';
11 our $VERSION = '0.13';
12 our $AUTHORITY = 'cpan:STEVAN';
14 use Moose::Meta::Class;
15 use Moose::Meta::Role::Method;
16 use Moose::Meta::Role::Method::Required;
18 use base 'Class::MOP::Module';
20 ## ------------------------------------------------------------------
22 ## I normally don't do this, but I am doing
23 ## a whole bunch of meta-programmin in this
24 ## module, so it just makes sense. For a clearer
25 ## picture of what is going on in the next
26 ## several lines of code, look at the really
27 ## big comment at the end of this file (right
30 ## ------------------------------------------------------------------
32 my $META = __PACKAGE__->meta;
34 ## ------------------------------------------------------------------
38 # since roles are lazy, we hold all the attributes
39 # of the individual role in 'statis' until which
40 # time when it is applied to a class. This means
41 # keeping a lot of things in hash maps, so we are
42 # using a little of that meta-programmin' magic
43 # here an saving lots of extra typin. And since
44 # many of these attributes above require similar
45 # functionality to support them, so we again use
46 # the wonders of meta-programmin' to deliver a
47 # very compact solution to this normally verbose
53 name => 'excluded_roles_map',
54 attr_reader => 'get_excluded_roles_map' ,
56 add => 'add_excluded_roles',
57 get_list => 'get_excluded_roles_list',
58 existence => 'excludes_role',
62 name => 'required_methods',
63 attr_reader => 'get_required_methods_map',
65 add => 'add_required_methods',
66 remove => 'remove_required_methods',
67 get_list => 'get_required_method_list',
68 existence => 'requires_method',
72 name => 'attribute_map',
73 attr_reader => 'get_attribute_map',
75 get => 'get_attribute',
76 get_list => 'get_attribute_list',
77 existence => 'has_attribute',
78 remove => 'remove_attribute',
83 my $attr_reader = $action->{attr_reader};
84 my $methods = $action->{methods};
86 # create the attribute
87 $META->add_attribute($action->{name} => (
88 reader => $attr_reader,
92 # create some helper methods
93 $META->add_method($methods->{add} => sub {
94 my ($self, @values) = @_;
95 $self->$attr_reader->{$_} = undef foreach @values;
96 }) if exists $methods->{add};
98 $META->add_method($methods->{get_list} => sub {
100 keys %{$self->$attr_reader};
101 }) if exists $methods->{get_list};
103 $META->add_method($methods->{get} => sub {
104 my ($self, $name) = @_;
105 $self->$attr_reader->{$name}
106 }) if exists $methods->{get};
108 $META->add_method($methods->{existence} => sub {
109 my ($self, $name) = @_;
110 exists $self->$attr_reader->{$name} ? 1 : 0;
111 }) if exists $methods->{existence};
113 $META->add_method($methods->{remove} => sub {
114 my ($self, @values) = @_;
115 delete $self->$attr_reader->{$_} foreach @values;
116 }) if exists $methods->{remove};
119 ## some things don't always fit, so they go here ...
125 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
131 $self->get_attribute_map->{$name} = $attr_desc;
135 # sub _clean_up_required_methods {
137 # foreach my $method ($self->get_required_method_list) {
138 # $self->remove_required_methods($method)
139 # if $self->has_method($method);
143 ## ------------------------------------------------------------------
147 # the before/around/after method modifiers are
148 # stored by name, but there can be many methods
149 # then associated with that name. So again we have
150 # lots of similar functionality, so we can do some
151 # meta-programmin' and save some time.
154 foreach my $modifier_type (qw[ before around after ]) {
156 my $attr_reader = "get_${modifier_type}_method_modifiers_map";
158 # create the attribute ...
159 $META->add_attribute("${modifier_type}_method_modifiers" => (
160 reader => $attr_reader,
161 default => sub { {} }
164 # and some helper methods ...
165 $META->add_method("get_${modifier_type}_method_modifiers" => sub {
166 my ($self, $method_name) = @_;
167 #return () unless exists $self->$attr_reader->{$method_name};
168 @{$self->$attr_reader->{$method_name}};
171 $META->add_method("has_${modifier_type}_method_modifiers" => sub {
172 my ($self, $method_name) = @_;
174 # for now we assume that if it exists,..
175 # it has at least one modifier in it
176 (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
179 $META->add_method("add_${modifier_type}_method_modifier" => sub {
180 my ($self, $method_name, $method) = @_;
182 $self->$attr_reader->{$method_name} = []
183 unless exists $self->$attr_reader->{$method_name};
185 my $modifiers = $self->$attr_reader->{$method_name};
188 # check to see that we aren't adding the
189 # same code twice. We err in favor of the
190 # first on here, this may not be as expected
191 foreach my $modifier (@{$modifiers}) {
192 return if $modifier == $method;
195 push @{$modifiers} => $method;
200 ## ------------------------------------------------------------------
201 ## override method mofidiers
203 $META->add_attribute('override_method_modifiers' => (
204 reader => 'get_override_method_modifiers_map',
205 default => sub { {} }
209 # these are a little different because there
210 # can only be one per name, whereas the other
211 # method modifiers can have multiples.
214 sub add_override_method_modifier {
215 my ($self, $method_name, $method) = @_;
216 (!$self->has_method($method_name))
217 || confess "Cannot add an override of method '$method_name' " .
218 "because there is a local version of '$method_name'";
219 $self->get_override_method_modifiers_map->{$method_name} = $method;
222 sub has_override_method_modifier {
223 my ($self, $method_name) = @_;
225 # for now we assume that if it exists,..
226 # it has at least one modifier in it
227 (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
230 sub get_override_method_modifier {
231 my ($self, $method_name) = @_;
232 $self->get_override_method_modifiers_map->{$method_name};
235 ## general list accessor ...
237 sub get_method_modifier_list {
238 my ($self, $modifier_type) = @_;
239 my $accessor = "get_${modifier_type}_method_modifiers_map";
240 keys %{$self->$accessor};
243 ## ------------------------------------------------------------------
246 __PACKAGE__->meta->add_attribute('roles' => (
247 reader => 'get_roles',
248 default => sub { [] }
252 my ($self, $role) = @_;
253 (blessed($role) && $role->isa('Moose::Meta::Role'))
254 || confess "Roles must be instances of Moose::Meta::Role";
255 push @{$self->get_roles} => $role;
258 sub calculate_all_roles {
264 $_->calculate_all_roles
265 } @{ $self->get_roles });
269 my ($self, $role_name) = @_;
271 || confess "You must supply a role name to look for";
272 # if we are it,.. then return true
273 return 1 if $role_name eq $self->name;
274 # otherwise.. check our children
275 foreach my $role (@{$self->get_roles}) {
276 return 1 if $role->does_role($role_name);
281 ## ------------------------------------------------------------------
284 sub method_metaclass { 'Moose::Meta::Role::Method' }
290 my $role_name = $self->name;
291 my $method_metaclass = $self->method_metaclass;
293 foreach my $symbol ($self->list_all_package_symbols('CODE')) {
295 my $code = $self->get_package_symbol('&' . $symbol);
297 my ($pkg, $name) = Class::MOP::get_code_info($code);
299 if ($pkg->can('meta')
301 # we don't know what ->meta we are calling
302 # here, so we need to be careful cause it
303 # just might blow up at us, or just complain
304 # loudly (in the case of Curses.pm) so we
305 # just be a little overly cautious here.
307 && eval { no warnings; blessed($pkg->meta) }
308 && $pkg->meta->isa('Moose::Meta::Role')) {
309 my $role = $pkg->meta->name;
310 next unless $self->does_role($role);
313 next if ($pkg || '') ne $role_name ||
314 (($name || '') ne '__ANON__' && ($pkg || '') ne $role_name);
317 $map->{$symbol} = $method_metaclass->wrap(
319 package_name => $role_name,
328 my ($self, $name) = @_;
329 $self->get_method_map->{$name};
333 my ($self, $name) = @_;
334 exists $self->get_method_map->{$name} ? 1 : 0
337 sub find_method_by_name { (shift)->get_method(@_) }
339 sub get_method_list {
341 grep { !/^meta$/ } keys %{$self->get_method_map};
345 my ($self, $method_name, $method) = @_;
346 (defined $method_name && $method_name)
347 || confess "You must define a method name";
349 my $body = (blessed($method) ? $method->body : $method);
350 ('CODE' eq (reftype($body) || ''))
351 || confess "Your code block must be a CODE reference";
353 $self->add_package_symbol("&${method_name}" => $body);
356 ## ------------------------------------------------------------------
358 ## ------------------------------------------------------------------
361 my ($self, $other, @args) = @_;
364 || confess "You must pass in an blessed instance";
366 if ($other->isa('Moose::Meta::Role')) {
367 require Moose::Meta::Role::Application::ToRole;
368 return Moose::Meta::Role::Application::ToRole->new(@args)->apply($self, $other);
370 elsif ($other->isa('Moose::Meta::Class')) {
371 require Moose::Meta::Role::Application::ToClass;
372 return Moose::Meta::Role::Application::ToClass->new(@args)->apply($self, $other);
375 require Moose::Meta::Role::Application::ToInstance;
376 return Moose::Meta::Role::Application::ToInstance->new(@args)->apply($self, $other);
381 my ($class, @role_specs) = @_;
383 require Moose::Meta::Role::Application::RoleSummation;
384 require Moose::Meta::Role::Composite;
386 my (@roles, %role_params);
387 while (@role_specs) {
388 my ($role, $params) = @{ splice @role_specs, 0, 1 };
389 push @roles => $role->meta;
390 next unless defined $params;
391 $role_params{$role} = $params;
394 my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
395 Moose::Meta::Role::Application::RoleSummation->new(
396 role_params => \%role_params
402 #####################################################################
404 ## This is Moose::Meta::Role as defined by Moose (plus the use of
405 ## MooseX::AttributeHelpers module). It is here as a reference to
406 ## make it easier to see what is happening above with all the meta
408 #####################################################################
411 # metaclass => 'Collection::Array',
412 # reader => 'get_roles',
413 # isa => 'ArrayRef[Moose::Meta::Roles]',
414 # default => sub { [] },
416 # 'push' => 'add_role',
420 # has 'excluded_roles_map' => (
421 # metaclass => 'Collection::Hash',
422 # reader => 'get_excluded_roles_map',
423 # isa => 'HashRef[Str]',
425 # # Not exactly set, cause it sets multiple
426 # 'set' => 'add_excluded_roles',
427 # 'keys' => 'get_excluded_roles_list',
428 # 'exists' => 'excludes_role',
432 # has 'attribute_map' => (
433 # metaclass => 'Collection::Hash',
434 # reader => 'get_attribute_map',
435 # isa => 'HashRef[Str]',
437 # # 'set' => 'add_attribute' # has some special crap in it
438 # 'get' => 'get_attribute',
439 # 'keys' => 'get_attribute_list',
440 # 'exists' => 'has_attribute',
441 # # Not exactly delete, cause it sets multiple
442 # 'delete' => 'remove_attribute',
446 # has 'required_methods' => (
447 # metaclass => 'Collection::Hash',
448 # reader => 'get_required_methods_map',
449 # isa => 'HashRef[Str]',
451 # # not exactly set, or delete since it works for multiple
452 # 'set' => 'add_required_methods',
453 # 'delete' => 'remove_required_methods',
454 # 'keys' => 'get_required_method_list',
455 # 'exists' => 'requires_method',
459 # # the before, around and after modifiers are
460 # # HASH keyed by method-name, with ARRAY of
461 # # CODE refs to apply in that order
463 # has 'before_method_modifiers' => (
464 # metaclass => 'Collection::Hash',
465 # reader => 'get_before_method_modifiers_map',
466 # isa => 'HashRef[ArrayRef[CodeRef]]',
468 # 'keys' => 'get_before_method_modifiers',
469 # 'exists' => 'has_before_method_modifiers',
470 # # This actually makes sure there is an
471 # # ARRAY at the given key, and pushed onto
472 # # it. It also checks for duplicates as well
473 # # 'add' => 'add_before_method_modifier'
477 # has 'after_method_modifiers' => (
478 # metaclass => 'Collection::Hash',
479 # reader =>'get_after_method_modifiers_map',
480 # isa => 'HashRef[ArrayRef[CodeRef]]',
482 # 'keys' => 'get_after_method_modifiers',
483 # 'exists' => 'has_after_method_modifiers',
484 # # This actually makes sure there is an
485 # # ARRAY at the given key, and pushed onto
486 # # it. It also checks for duplicates as well
487 # # 'add' => 'add_after_method_modifier'
491 # has 'around_method_modifiers' => (
492 # metaclass => 'Collection::Hash',
493 # reader =>'get_around_method_modifiers_map',
494 # isa => 'HashRef[ArrayRef[CodeRef]]',
496 # 'keys' => 'get_around_method_modifiers',
497 # 'exists' => 'has_around_method_modifiers',
498 # # This actually makes sure there is an
499 # # ARRAY at the given key, and pushed onto
500 # # it. It also checks for duplicates as well
501 # # 'add' => 'add_around_method_modifier'
505 # # override is similar to the other modifiers
506 # # except that it is not an ARRAY of code refs
507 # # but instead just a single name->code mapping
509 # has 'override_method_modifiers' => (
510 # metaclass => 'Collection::Hash',
511 # reader =>'get_override_method_modifiers_map',
512 # isa => 'HashRef[CodeRef]',
514 # 'keys' => 'get_override_method_modifier',
515 # 'exists' => 'has_override_method_modifier',
516 # 'add' => 'add_override_method_modifier', # checks for local method ..
520 #####################################################################
531 Moose::Meta::Role - The Moose Role metaclass
535 Please see L<Moose::Role> for more information about roles.
536 For the most part, this has no user-serviceable parts inside
537 this module. It's API is still subject to some change (although
538 probably not that much really).
576 =item B<add_excluded_roles>
578 =item B<excludes_role>
580 =item B<get_excluded_roles_list>
582 =item B<get_excluded_roles_map>
584 =item B<calculate_all_roles>
590 =item B<method_metaclass>
592 =item B<find_method_by_name>
598 =item B<alias_method>
600 =item B<get_method_list>
602 =item B<get_method_map>
604 =item B<update_package_cache_flag>
606 =item B<reset_package_cache_flag>
612 =item B<add_attribute>
614 =item B<has_attribute>
616 =item B<get_attribute>
618 =item B<get_attribute_list>
620 =item B<get_attribute_map>
622 =item B<remove_attribute>
628 =item B<add_required_methods>
630 =item B<remove_required_methods>
632 =item B<get_required_method_list>
634 =item B<get_required_methods_map>
636 =item B<requires_method>
642 =item B<add_after_method_modifier>
644 =item B<add_around_method_modifier>
646 =item B<add_before_method_modifier>
648 =item B<add_override_method_modifier>
654 =item B<has_after_method_modifiers>
656 =item B<has_around_method_modifiers>
658 =item B<has_before_method_modifiers>
660 =item B<has_override_method_modifier>
666 =item B<get_after_method_modifiers>
668 =item B<get_around_method_modifiers>
670 =item B<get_before_method_modifiers>
672 =item B<get_method_modifier_list>
678 =item B<get_override_method_modifier>
680 =item B<get_after_method_modifiers_map>
682 =item B<get_around_method_modifiers_map>
684 =item B<get_before_method_modifiers_map>
686 =item B<get_override_method_modifiers_map>
692 All complex software has bugs lurking in it, and this module is no
693 exception. If you find a bug please either email me, or add the bug
698 Stevan Little E<lt>stevan@iinteractive.comE<gt>
700 =head1 COPYRIGHT AND LICENSE
702 Copyright 2006-2008 by Infinity Interactive, Inc.
704 L<http://www.iinteractive.com>
706 This library is free software; you can redistribute it and/or modify
707 it under the same terms as Perl itself.