1 package Mouse::Meta::Role;
5 use Mouse::Util qw(not_supported english_list);
6 use Mouse::Meta::Module;
8 use base qw(Mouse::Meta::Module);
10 sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
17 $args{methods} ||= {};
18 $args{attributes} ||= {};
19 $args{required_methods} ||= [];
22 # return Mouse::Meta::Class->initialize($class)->new_object(%args)
23 # if $class ne __PACKAGE__;
25 return bless \%args, ref($class) || $class;
30 return $self->create(undef, @_);
34 return exists $_[0]->{anon_serial_id};
37 sub get_roles { $_[0]->{roles} }
39 sub get_required_method_list{
40 return @{ $_[0]->{required_methods} };
43 sub add_required_methods {
44 my($self, @methods) = @_;
45 push @{$self->{required_methods}}, @methods;
49 my($self, $name) = @_;
50 return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
57 $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
60 sub _canonicalize_apply_args{
61 my($self, $applicant, %args) = @_;
63 if($applicant->isa('Mouse::Meta::Class')){
66 elsif($applicant->isa('Mouse::Meta::Role')){
70 $args{_to} = 'instance';
72 not_supported 'Application::ToInstance';
75 if($args{alias} && !exists $args{-alias}){
76 $args{-alias} = $args{alias};
78 if($args{excludes} && !exists $args{-excludes}){
79 $args{-excludes} = $args{excludes};
82 if(my $excludes = $args{-excludes}){
83 $args{-excludes} = {}; # replace with a hash ref
85 %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
88 $args{-excludes}{$excludes} = undef;
95 sub _check_required_methods{
96 my($role, $class, $args, @other_roles) = @_;
98 if($args->{_to} eq 'class'){
99 my $class_name = $class->name;
100 my $role_name = $role->name;
102 foreach my $method_name(@{$role->{required_methods}}){
103 if(!$class_name->can($method_name)){
106 foreach my $another_role_spec(@other_roles){
107 my $another_role_name = $another_role_spec->[0];
108 if($role_name ne $another_role_name && $another_role_name->can($method_name)){
114 push @missing, $method_name if !$has_method;
118 $class->throw_error("'$role_name' requires the "
119 . (@missing == 1 ? 'method' : 'methods')
121 . english_list(map{ sprintf q{'%s'}, $_ } @missing)
122 . " to be implemented by '$class_name'");
125 elsif($args->{_to} eq 'role'){
126 # apply role($role) to role($class)
127 foreach my $method_name($role->get_required_method_list){
128 next if $class->has_method($method_name); # already has it
129 $class->add_required_methods($method_name);
137 my($role, $class, $args) = @_;
139 my $role_name = $role->name;
140 my $class_name = $class->name;
142 my $alias = $args->{-alias};
143 my $excludes = $args->{-excludes};
145 foreach my $method_name($role->get_method_list){
146 next if $method_name eq 'meta';
148 my $code = $role_name->can($method_name);
150 if(!exists $excludes->{$method_name}){
151 if(!$class->has_method($method_name)){
152 $class->add_method($method_name => $code);
156 if($alias && $alias->{$method_name}){
157 my $dstname = $alias->{$method_name};
159 my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} };
161 if(defined($dstcode) && $dstcode != $code){
162 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
165 $class->add_method($dstname => $code);
173 sub _apply_attributes{
174 my($role, $class, $args) = @_;
176 if ($args->{_to} eq 'class') {
177 # apply role to class
178 for my $attr_name ($role->get_attribute_list) {
179 next if $class->has_attribute($attr_name);
181 my $spec = $role->get_attribute($attr_name);
183 $class->add_attribute($attr_name => %{$spec});
186 elsif($args->{_to} eq 'role'){
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);
192 $class->add_attribute($attr_name => $spec);
199 sub _apply_modifiers{
200 my($role, $class, $args) = @_;
202 for my $modifier_type (qw/override before around after/) {
203 my $add_modifier = "add_${modifier_type}_method_modifier";
204 my $modifiers = $role->{"${modifier_type}_method_modifiers"};
206 while(my($method_name, $modifier_codes) = each %{$modifiers}){
207 foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
208 $class->$add_modifier($method_name => $code);
216 my($role, $class, $args) = @_;
218 my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles;
220 foreach my $r($role, @{$role->get_roles}){
221 if(!$class->does_role($r->name)){
228 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
231 my $applicant = shift;
233 my $args = $self->_canonicalize_apply_args($applicant, @_);
235 $self->_check_required_methods($applicant, $args);
236 $self->_apply_methods($applicant, $args);
237 $self->_apply_attributes($applicant, $args);
238 $self->_apply_modifiers($applicant, $args);
239 $self->_append_roles($applicant, $args);
244 my(undef, $class, @roles) = @_;
246 if($class->isa('Mouse::Object')){
247 not_supported 'Application::ToInstance';
252 my @method_conflicts;
254 my %override_provided;
256 foreach my $role_spec (@roles) {
257 my $role = $role_spec->[0]->meta;
258 my $role_name = $role->name;
261 foreach my $method_name($role->get_method_list){
262 next if $class->has_method($method_name); # manually resolved
264 my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } };
266 my $c = $method_provided{$method_name};
268 if($c && $c->[0] != $code){
270 push @method_conflicts, $c;
273 $method_provided{$method_name} = [$code, $method_name, $role];
278 foreach my $attr_name($role->get_attribute_list){
279 my $attr = $role->get_attribute($attr_name);
280 my $c = $attr_provided{$attr_name};
281 if($c && $c != $attr){
282 $class->throw_error("We have encountered an attribute conflict with '$attr_name' "
283 . "during composition. This is fatal error and cannot be disambiguated.")
286 $attr_provided{$attr_name} = $attr;
291 foreach my $method_name($role->get_method_modifier_list('override')){
292 my $override = $role->get_override_method_modifier($method_name);
293 my $c = $override_provided{$method_name};
294 if($c && $c != $override){
295 $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
296 . "composition (Two 'override' methods of the same name encountered). "
297 . "This is fatal error.")
300 $override_provided{$method_name} = $override;
304 if(@method_conflicts){
307 if(@method_conflicts == 1){
308 my($code, $method_name, @roles) = @{$method_conflicts[0]};
310 sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'},
311 english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name
315 @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs
316 my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts);
317 my $roles = english_list(
318 map{ sprintf q{'%s'}, $_->name }
319 map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts
323 sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'},
324 $roles, $methods, $class->name
329 foreach my $role_spec (@roles) {
330 my($role_name, $args) = @{$role_spec};
332 my $role = $role_name->meta;
334 $args = $role->_canonicalize_apply_args($class, %{$args});
336 $role->_check_required_methods($class, $args, @roles);
337 $role->_apply_methods($class, $args);
338 $role->_apply_attributes($class, $args);
339 $role->_apply_modifiers($class, $args);
340 $role->_append_roles($class, $args);
345 for my $modifier_type (qw/before after around/) {
347 my $modifier = "${modifier_type}_method_modifiers";
348 my $add_method_modifier = sub {
349 my ($self, $method_name, $method) = @_;
351 push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
354 my $has_method_modifiers = sub{
355 my($self, $method_name) = @_;
356 my $m = $self->{$modifier}->{$method_name};
357 return $m && @{$m} != 0;
359 my $get_method_modifiers = sub {
360 my ($self, $method_name) = @_;
361 return @{ $self->{$modifier}->{$method_name} ||= [] }
365 *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier;
366 *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
367 *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
370 sub add_override_method_modifier{
371 my($self, $method_name, $method) = @_;
373 if($self->has_method($method_name)){
374 # This error happens in the override keyword or during role composition,
375 # so I added a message, "A local method of ...", only for compatibility (gfx)
376 $self->throw_error("Cannot add an override of method '$method_name' "
377 . "because there is a local version of '$method_name'"
378 . "(A local method of the same name as been found)");
381 $self->{override_method_modifiers}->{$method_name} = $method;
384 sub has_override_method_modifier {
385 my ($self, $method_name) = @_;
386 return exists $self->{override_method_modifiers}->{$method_name};
389 sub get_override_method_modifier {
390 my ($self, $method_name) = @_;
391 return $self->{override_method_modifiers}->{$method_name};
394 sub get_method_modifier_list {
395 my($self, $modifier_type) = @_;
397 return keys %{ $self->{$modifier_type . '_method_modifiers'} };
400 # This is currently not passing all the Moose tests.
402 my ($self, $role_name) = @_;
405 || $self->throw_error("You must supply a role name to look for");
407 # if we are it,.. then return true
408 return 1 if $role_name eq $self->name;
409 # otherwise.. check our children
410 for my $role (@{ $self->get_roles }) {
411 return 1 if $role->does_role($role_name);
423 Mouse::Meta::Role - The Mouse Role metaclass