X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FRole.pm;h=c030b702c4c4e4143b94ada1156f44cbf0945eb4;hp=5d1c57fea6de718f53a6aaf627c3be03fa062797;hb=f3e1112299b631f1ab6150159eaa414dca37a42c;hpb=9009aca113962c65c836364c6d7d2e7e382bd888 diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 5d1c57f..c030b70 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -1,22 +1,20 @@ package Mouse::Meta::Role; -use strict; -use warnings; +use Mouse::Util qw(:meta not_supported); # enables strict and warnings -use Mouse::Util qw(:meta not_supported english_list get_code_info); use Mouse::Meta::Module; our @ISA = qw(Mouse::Meta::Module); -sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method() +sub method_metaclass; sub _construct_meta { my $class = shift; my %args = @_; - $args{methods} ||= {}; - $args{attributes} ||= {}; - $args{required_methods} ||= []; - $args{roles} ||= []; + $args{methods} = {}; + $args{attributes} = {}; + $args{required_methods} = []; + $args{roles} = []; my $self = bless \%args, ref($class) || $class; if($class ne __PACKAGE__){ @@ -31,11 +29,16 @@ sub create_anon_role{ return $self->create(undef, @_); } -sub is_anon_role{ - return exists $_[0]->{anon_serial_id}; -} +sub is_anon_role; + +sub get_roles; -sub get_roles { $_[0]->{roles} } +sub calculate_all_roles { + my $self = shift; + my %seen; + return grep { !$seen{ $_->name }++ } + ($self, map { $_->calculate_all_roles } @{ $self->get_roles }); +} sub get_required_method_list{ return @{ $_[0]->{required_methods} }; @@ -43,7 +46,9 @@ sub get_required_method_list{ sub add_required_methods { my($self, @methods) = @_; - push @{$self->{required_methods}}, @methods; + my %required = map{ $_ => 1 } @{$self->{required_methods}}; + push @{$self->{required_methods}}, grep{ !$required{$_}++ && !$self->has_method($_) } @methods; + return; } sub requires_method { @@ -56,78 +61,32 @@ sub add_attribute { my $name = shift; $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ }; + return; } -sub _canonicalize_apply_args{ - my($self, $applicant, %args) = @_; - - if($applicant->isa('Mouse::Meta::Class')){ - $args{_to} = 'class'; - } - elsif($applicant->isa('Mouse::Meta::Role')){ - $args{_to} = 'role'; - } - else{ - $args{_to} = 'instance'; - - not_supported 'Application::ToInstance'; - } - - if($args{alias} && !exists $args{-alias}){ - $args{-alias} = $args{alias}; - } - if($args{excludes} && !exists $args{-excludes}){ - $args{-excludes} = $args{excludes}; - } +sub _check_required_methods{ + my($role, $consumer, $args) = @_; - if(my $excludes = $args{-excludes}){ - $args{-excludes} = {}; # replace with a hash ref - if(ref $excludes){ - %{$args{-excludes}} = (map{ $_ => undef } @{$excludes}); - } - else{ - $args{-excludes}{$excludes} = undef; - } + if($args->{_to} eq 'role'){ + $consumer->add_required_methods($role->get_required_method_list); } + else{ # to class or instance + my $consumer_class_name = $consumer->name; - return \%args; -} - -sub _check_required_methods{ - my($role, $class, $args, @other_roles) = @_; - - if($args->{_to} eq 'class'){ - my $class_name = $class->name; - my $role_name = $role->name; my @missing; foreach my $method_name(@{$role->{required_methods}}){ - if(!$class_name->can($method_name)){ - my $has_method = 0; - - foreach my $another_role_spec(@other_roles){ - my $another_role_name = $another_role_spec->[0]; - if($role_name ne $another_role_name && $another_role_name->can($method_name)){ - $has_method = 1; - last; - } - } - - push @missing, $method_name if !$has_method; - } + next if exists $args->{aliased_methods}{$method_name}; + next if exists $role->{methods}{$method_name}; + next if $consumer_class_name->can($method_name); + + push @missing, $method_name; } if(@missing){ - $class->throw_error("'$role_name' requires the " - . (@missing == 1 ? 'method' : 'methods') - . " " - . english_list(map{ sprintf q{'%s'}, $_ } @missing) - . " to be implemented by '$class_name'"); - } - } - elsif($args->{_to} eq 'role'){ - # apply role($role) to role($class) - foreach my $method_name($role->get_required_method_list){ - next if $class->has_method($method_name); # already has it - $class->add_required_methods($method_name); + $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'", + $role->name, + (@missing == 1 ? '' : 's'), # method or methods + Mouse::Util::quoted_english_list(@missing), + $consumer_class_name); } } @@ -135,10 +94,7 @@ sub _check_required_methods{ } sub _apply_methods{ - my($role, $class, $args) = @_; - - my $role_name = $role->name; - my $class_name = $class->name; + my($role, $consumer, $args) = @_; my $alias = $args->{-alias}; my $excludes = $args->{-excludes}; @@ -146,24 +102,25 @@ sub _apply_methods{ foreach my $method_name($role->get_method_list){ next if $method_name eq 'meta'; - my $code = $role_name->can($method_name); + my $code = $role->get_method_body($method_name); if(!exists $excludes->{$method_name}){ - if(!$class->has_method($method_name)){ - $class->add_method($method_name => $code); + if(!$consumer->has_method($method_name)){ + # The third argument $role is used in Role::Composite + $consumer->add_method($method_name => $code, $role); } } - if($alias && $alias->{$method_name}){ + if(exists $alias->{$method_name}){ my $dstname = $alias->{$method_name}; - my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} }; + my $dstcode = $consumer->get_method_body($dstname); if(defined($dstcode) && $dstcode != $code){ - $class->throw_error("Cannot create a method alias if a local method of the same name exists"); + $role->throw_error("Cannot create a method alias if a local method of the same name exists"); } else{ - $class->add_method($dstname => $code); + $consumer->add_method($dstname => $code, $role); } } } @@ -172,41 +129,35 @@ sub _apply_methods{ } sub _apply_attributes{ - my($role, $class, $args) = @_; - - if ($args->{_to} eq 'class') { - # apply role to class - for my $attr_name ($role->get_attribute_list) { - next if $class->has_attribute($attr_name); + my($role, $consumer, $args) = @_; - my $spec = $role->get_attribute($attr_name); + for my $attr_name ($role->get_attribute_list) { + next if $consumer->has_attribute($attr_name); - $class->add_attribute($attr_name => %{$spec}); - } + $consumer->add_attribute($attr_name => $role->get_attribute($attr_name)); } - elsif($args->{_to} eq 'role'){ - # apply role to role - for my $attr_name ($role->get_attribute_list) { - next if $class->has_attribute($attr_name); - - my $spec = $role->get_attribute($attr_name); - $class->add_attribute($attr_name => $spec); - } - } - return; } sub _apply_modifiers{ - my($role, $class, $args) = @_; + my($role, $consumer, $args) = @_; + + if(my $modifiers = $role->{override_method_modifiers}){ + foreach my $method_name (keys %{$modifiers}){ + $consumer->add_override_method_modifier($method_name => $modifiers->{$method_name}); + } + } + + for my $modifier_type (qw/before around after/) { + my $table = $role->{"${modifier_type}_method_modifiers"} + or next; - for my $modifier_type (qw/override before around after/) { my $add_modifier = "add_${modifier_type}_method_modifier"; - my $modifiers = $role->{"${modifier_type}_method_modifiers"}; - while(my($method_name, $modifier_codes) = each %{$modifiers}){ - foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){ - $class->$add_modifier($method_name => $code); + while(my($method_name, $modifiers) = each %{$table}){ + foreach my $code(@{ $modifiers }){ + next if $consumer->{"_applied_$modifier_type"}{$method_name, $code}++; # skip applied modifiers + $consumer->$add_modifier($method_name => $code); } } } @@ -214,12 +165,12 @@ sub _apply_modifiers{ } sub _append_roles{ - my($role, $class, $args) = @_; + my($role, $consumer, $args) = @_; - my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles; + my $roles = $consumer->{roles}; foreach my $r($role, @{$role->get_roles}){ - if(!$class->does_role($r->name)){ + if(!$consumer->does_role($r)){ push @{$roles}, $r; } } @@ -228,146 +179,90 @@ sub _append_roles{ # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole sub apply { - my $self = shift; - my $applicant = shift; - - my $args = $self->_canonicalize_apply_args($applicant, @_); + my $self = shift; + my $consumer = shift; - $self->_check_required_methods($applicant, $args); - $self->_apply_methods($applicant, $args); - $self->_apply_attributes($applicant, $args); - $self->_apply_modifiers($applicant, $args); - $self->_append_roles($applicant, $args); - return; -} + my %args = (@_ == 1) ? %{ $_[0] } : @_; -sub combine_apply { - my(undef, $class, @roles) = @_; + my $instance; - if($class->isa('Mouse::Object')){ - not_supported 'Application::ToInstance'; + if(Mouse::Util::is_a_metaclass($consumer)){ # Application::ToClass + $args{_to} = 'class'; } + elsif(Mouse::Util::is_a_metarole($consumer)){ # Application::ToRole + $args{_to} = 'role'; + } + else{ # Appplication::ToInstance + $args{_to} = 'instance'; + $instance = $consumer; - # check conflicting - my %method_provided; - my @method_conflicts; - my %attr_provided; - my %override_provided; - - foreach my $role_spec (@roles) { - my $role = $role_spec->[0]->meta; - my $role_name = $role->name; - - # methods - foreach my $method_name($role->get_method_list){ - next if $class->has_method($method_name); # manually resolved - - my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } }; - - my $c = $method_provided{$method_name}; - - if($c && $c->[0] != $code){ - push @{$c}, $role; - push @method_conflicts, $c; - } - else{ - $method_provided{$method_name} = [$code, $method_name, $role]; - } - } + $consumer = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class( + superclasses => [ref $instance], + cache => 1, + ); + } - # attributes - foreach my $attr_name($role->get_attribute_list){ - my $attr = $role->get_attribute($attr_name); - my $c = $attr_provided{$attr_name}; - if($c && $c != $attr){ - $class->throw_error("We have encountered an attribute conflict with '$attr_name' " - . "during composition. This is fatal error and cannot be disambiguated.") - } - else{ - $attr_provided{$attr_name} = $attr; - } - } + if($args{alias} && !exists $args{-alias}){ + $args{-alias} = $args{alias}; + } + if($args{excludes} && !exists $args{-excludes}){ + $args{-excludes} = $args{excludes}; + } - # override modifiers - foreach my $method_name($role->get_method_modifier_list('override')){ - my $override = $role->get_override_method_modifier($method_name); - my $c = $override_provided{$method_name}; - if($c && $c != $override){ - $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during " - . "composition (Two 'override' methods of the same name encountered). " - . "This is fatal error.") - } - else{ - $override_provided{$method_name} = $override; - } - } + $args{aliased_methods} = {}; + if(my $alias = $args{-alias}){ + @{$args{aliased_methods}}{ values %{$alias} } = (); } - if(@method_conflicts){ - my $error; - - if(@method_conflicts == 1){ - my($code, $method_name, @roles) = @{$method_conflicts[0]}; - $class->throw_error( - sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'}, - english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name - ); + + if(my $excludes = $args{-excludes}){ + $args{-excludes} = {}; # replace with a hash ref + if(ref $excludes){ + %{$args{-excludes}} = (map{ $_ => undef } @{$excludes}); } else{ - @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs - my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts); - my $roles = english_list( - map{ sprintf q{'%s'}, $_->name } - map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts - ); - - $class->throw_error( - sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'}, - $roles, $methods, $class->name - ); + $args{-excludes}{$excludes} = undef; } } - foreach my $role_spec (@roles) { - my($role_name, $args) = @{$role_spec}; - - my $role = $role_name->meta; + $self->_check_required_methods($consumer, \%args); + $self->_apply_attributes($consumer, \%args); + $self->_apply_methods($consumer, \%args); + $self->_apply_modifiers($consumer, \%args); + $self->_append_roles($consumer, \%args); - $args = $role->_canonicalize_apply_args($class, %{$args}); - $role->_check_required_methods($class, $args, @roles); - $role->_apply_methods($class, $args); - $role->_apply_attributes($class, $args); - $role->_apply_modifiers($class, $args); - $role->_append_roles($class, $args); + if(defined $instance){ # Application::ToInstance + # rebless instance + bless $instance, $consumer->name; + $consumer->_initialize_object($instance, $instance); } + return; } -for my $modifier_type (qw/before after around/) { - - my $modifier = "${modifier_type}_method_modifiers"; - my $add_method_modifier = sub { - my ($self, $method_name, $method) = @_; - - push @{ $self->{$modifier}->{$method_name} ||= [] }, $method; - return; - }; - my $has_method_modifiers = sub{ - my($self, $method_name) = @_; - my $m = $self->{$modifier}->{$method_name}; - return $m && @{$m} != 0; - }; - my $get_method_modifiers = sub { - my ($self, $method_name) = @_; - return @{ $self->{$modifier}->{$method_name} ||= [] } - }; - - no strict 'refs'; - *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier; - *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers; - *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers; + +sub combine { + my($role_class, @role_specs) = @_; + + require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace + + my $composite = Mouse::Meta::Role::Composite->create_anon_role(); + + foreach my $role_spec (@role_specs) { + my($role_name, $args) = @{$role_spec}; + $role_name->meta->apply($composite, %{$args}); + } + return $composite; } +sub add_before_method_modifier; +sub add_around_method_modifier; +sub add_after_method_modifier; + +sub get_before_method_modifiers; +sub get_around_method_modifiers; +sub get_after_method_modifiers; + sub add_override_method_modifier{ my($self, $method_name, $method) = @_; @@ -382,29 +277,19 @@ sub add_override_method_modifier{ $self->{override_method_modifiers}->{$method_name} = $method; } -sub has_override_method_modifier { - my ($self, $method_name) = @_; - return exists $self->{override_method_modifiers}->{$method_name}; -} - sub get_override_method_modifier { my ($self, $method_name) = @_; return $self->{override_method_modifiers}->{$method_name}; } -sub get_method_modifier_list { - my($self, $modifier_type) = @_; - - return keys %{ $self->{$modifier_type . '_method_modifiers'} }; -} - -# This is currently not passing all the Moose tests. sub does_role { my ($self, $role_name) = @_; (defined $role_name) || $self->throw_error("You must supply a role name to look for"); + $role_name = $role_name->name if ref $role_name; + # if we are it,.. then return true return 1 if $role_name eq $self->name; # otherwise.. check our children @@ -414,15 +299,17 @@ sub does_role { return 0; } - 1; - __END__ =head1 NAME Mouse::Meta::Role - The Mouse Role metaclass +=head1 VERSION + +This document describes Mouse version 0.50_03 + =head1 SEE ALSO L