1 package Mouse::Meta::Role;
5 use Mouse::Util qw(:meta not_supported english_list get_code_info);
6 use Mouse::Meta::Module;
7 our @ISA = qw(Mouse::Meta::Module);
9 sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
11 # XXX: for backward compatibility
12 my %foreign = map{ $_ => undef } qw(Mouse::Role Carp Scalar::Util UNIVERSAL);
14 my($self, $code) = @_;
16 my($package, $name) = get_code_info($code);
18 return $package && !exists $foreign{$package};
27 $args{methods} ||= {};
28 $args{attributes} ||= {};
29 $args{required_methods} ||= [];
32 # return Mouse::Meta::Class->initialize($class)->new_object(%args)
33 # if $class ne __PACKAGE__;
35 return bless \%args, ref($class) || $class;
40 return $self->create(undef, @_);
44 return exists $_[0]->{anon_serial_id};
47 sub get_roles { $_[0]->{roles} }
49 sub get_required_method_list{
50 return @{ $_[0]->{required_methods} };
53 sub add_required_methods {
54 my($self, @methods) = @_;
55 push @{$self->{required_methods}}, @methods;
59 my($self, $name) = @_;
60 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
67 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
70 sub _canonicalize_apply_args{
71 my($self, $applicant, %args) = @_;
73 if($applicant->isa('Mouse::Meta::Class')){
76 elsif($applicant->isa('Mouse::Meta::Role')){
80 $args{_to} = 'instance';
82 not_supported 'Application::ToInstance';
85 if($args{alias} && !exists $args{-alias}){
86 $args{-alias} = $args{alias};
88 if($args{excludes} && !exists $args{-excludes}){
89 $args{-excludes} = $args{excludes};
92 if(my $excludes = $args{-excludes}){
93 $args{-excludes} = {}; # replace with a hash ref
95 %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
98 $args{-excludes}{$excludes} = undef;
105 sub _check_required_methods{
106 my($role, $class, $args, @other_roles) = @_;
108 if($args->{_to} eq 'class'){
109 my $class_name = $class->name;
110 my $role_name = $role->name;
112 foreach my $method_name(@{$role->{required_methods}}){
113 if(!$class_name->can($method_name)){
116 foreach my $another_role_spec(@other_roles){
117 my $another_role_name = $another_role_spec->[0];
118 if($role_name ne $another_role_name && $another_role_name->can($method_name)){
124 push @missing, $method_name if !$has_method;
128 $class->throw_error("'$role_name' requires the "
129 . (@missing == 1 ? 'method' : 'methods')
131 . english_list(map{ sprintf q{'%s'}, $_ } @missing)
132 . " to be implemented by '$class_name'");
135 elsif($args->{_to} eq 'role'){
136 # apply role($role) to role($class)
137 foreach my $method_name($role->get_required_method_list){
138 next if $class->has_method($method_name); # already has it
139 $class->add_required_methods($method_name);
147 my($role, $class, $args) = @_;
149 my $role_name = $role->name;
150 my $class_name = $class->name;
152 my $alias = $args->{-alias};
153 my $excludes = $args->{-excludes};
155 foreach my $method_name($role->get_method_list){
156 next if $method_name eq 'meta';
158 my $code = $role_name->can($method_name);
160 if(!exists $excludes->{$method_name}){
161 if(!$class->has_method($method_name)){
162 $class->add_method($method_name => $code);
166 if($alias && $alias->{$method_name}){
167 my $dstname = $alias->{$method_name};
169 my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} };
171 if(defined($dstcode) && $dstcode != $code){
172 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
175 $class->add_method($dstname => $code);
183 sub _apply_attributes{
184 my($role, $class, $args) = @_;
186 if ($args->{_to} eq 'class') {
187 # apply role to class
188 for my $attr_name ($role->get_attribute_list) {
189 next if $class->has_attribute($attr_name);
191 my $spec = $role->get_attribute($attr_name);
193 $class->add_attribute($attr_name => %{$spec});
196 elsif($args->{_to} eq 'role'){
198 for my $attr_name ($role->get_attribute_list) {
199 next if $class->has_attribute($attr_name);
201 my $spec = $role->get_attribute($attr_name);
202 $class->add_attribute($attr_name => $spec);
209 sub _apply_modifiers{
210 my($role, $class, $args) = @_;
212 for my $modifier_type (qw/override before around after/) {
213 my $add_modifier = "add_${modifier_type}_method_modifier";
214 my $modifiers = $role->{"${modifier_type}_method_modifiers"};
216 while(my($method_name, $modifier_codes) = each %{$modifiers}){
217 foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
218 $class->$add_modifier($method_name => $code);
226 my($role, $class, $args) = @_;
228 my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles;
230 foreach my $r($role, @{$role->get_roles}){
231 if(!$class->does_role($r->name)){
238 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
241 my $applicant = shift;
243 my $args = $self->_canonicalize_apply_args($applicant, @_);
245 $self->_check_required_methods($applicant, $args);
246 $self->_apply_methods($applicant, $args);
247 $self->_apply_attributes($applicant, $args);
248 $self->_apply_modifiers($applicant, $args);
249 $self->_append_roles($applicant, $args);
254 my(undef, $class, @roles) = @_;
256 if($class->isa('Mouse::Object')){
257 not_supported 'Application::ToInstance';
262 my @method_conflicts;
264 my %override_provided;
266 foreach my $role_spec (@roles) {
267 my $role = $role_spec->[0]->meta;
268 my $role_name = $role->name;
271 foreach my $method_name($role->get_method_list){
272 next if $class->has_method($method_name); # manually resolved
274 my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } };
276 my $c = $method_provided{$method_name};
278 if($c && $c->[0] != $code){
280 push @method_conflicts, $c;
283 $method_provided{$method_name} = [$code, $method_name, $role];
288 foreach my $attr_name($role->get_attribute_list){
289 my $attr = $role->get_attribute($attr_name);
290 my $c = $attr_provided{$attr_name};
291 if($c && $c != $attr){
292 $class->throw_error("We have encountered an attribute conflict with '$attr_name' "
293 . "during composition. This is fatal error and cannot be disambiguated.")
296 $attr_provided{$attr_name} = $attr;
301 foreach my $method_name($role->get_method_modifier_list('override')){
302 my $override = $role->get_override_method_modifier($method_name);
303 my $c = $override_provided{$method_name};
304 if($c && $c != $override){
305 $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
306 . "composition (Two 'override' methods of the same name encountered). "
307 . "This is fatal error.")
310 $override_provided{$method_name} = $override;
314 if(@method_conflicts){
317 if(@method_conflicts == 1){
318 my($code, $method_name, @roles) = @{$method_conflicts[0]};
320 sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'},
321 english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name
325 @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs
326 my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts);
327 my $roles = english_list(
328 map{ sprintf q{'%s'}, $_->name }
329 map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts
333 sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'},
334 $roles, $methods, $class->name
339 foreach my $role_spec (@roles) {
340 my($role_name, $args) = @{$role_spec};
342 my $role = $role_name->meta;
344 $args = $role->_canonicalize_apply_args($class, %{$args});
346 $role->_check_required_methods($class, $args, @roles);
347 $role->_apply_methods($class, $args);
348 $role->_apply_attributes($class, $args);
349 $role->_apply_modifiers($class, $args);
350 $role->_append_roles($class, $args);
355 for my $modifier_type (qw/before after around/) {
357 my $modifier = "${modifier_type}_method_modifiers";
358 my $add_method_modifier = sub {
359 my ($self, $method_name, $method) = @_;
361 push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
364 my $has_method_modifiers = sub{
365 my($self, $method_name) = @_;
366 my $m = $self->{$modifier}->{$method_name};
367 return $m && @{$m} != 0;
369 my $get_method_modifiers = sub {
370 my ($self, $method_name) = @_;
371 return @{ $self->{$modifier}->{$method_name} ||= [] }
375 *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier;
376 *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
377 *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
380 sub add_override_method_modifier{
381 my($self, $method_name, $method) = @_;
383 if($self->has_method($method_name)){
384 # This error happens in the override keyword or during role composition,
385 # so I added a message, "A local method of ...", only for compatibility (gfx)
386 $self->throw_error("Cannot add an override of method '$method_name' "
387 . "because there is a local version of '$method_name'"
388 . "(A local method of the same name as been found)");
391 $self->{override_method_modifiers}->{$method_name} = $method;
394 sub has_override_method_modifier {
395 my ($self, $method_name) = @_;
396 return exists $self->{override_method_modifiers}->{$method_name};
399 sub get_override_method_modifier {
400 my ($self, $method_name) = @_;
401 return $self->{override_method_modifiers}->{$method_name};
404 sub get_method_modifier_list {
405 my($self, $modifier_type) = @_;
407 return keys %{ $self->{$modifier_type . '_method_modifiers'} };
410 # This is currently not passing all the Moose tests.
412 my ($self, $role_name) = @_;
415 || $self->throw_error("You must supply a role name to look for");
417 # if we are it,.. then return true
418 return 1 if $role_name eq $self->name;
419 # otherwise.. check our children
420 for my $role (@{ $self->get_roles }) {
421 return 1 if $role->does_role($role_name);
433 Mouse::Meta::Role - The Mouse Role metaclass