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=a3d3544f664d3b25e76b504890aa76a4060ff73e;hb=f3e1112299b631f1ab6150159eaa414dca37a42c;hpb=f774b7de063779726b9f84c70f512cab94fee065 diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index a3d3544..c030b70 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -1,20 +1,20 @@ package Mouse::Meta::Role; -use Mouse::Util qw(:meta not_supported english_list); # enables strict and warnings +use Mouse::Util qw(:meta not_supported); # enables strict and warnings 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__){ @@ -29,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} }; @@ -60,19 +65,19 @@ sub add_attribute { } sub _check_required_methods{ - my($role, $applicant, $args) = @_; + my($role, $consumer, $args) = @_; if($args->{_to} eq 'role'){ - $applicant->add_required_methods($role->get_required_method_list); + $consumer->add_required_methods($role->get_required_method_list); } else{ # to class or instance - my $applicant_class_name = $applicant->name; + my $consumer_class_name = $consumer->name; my @missing; foreach my $method_name(@{$role->{required_methods}}){ next if exists $args->{aliased_methods}{$method_name}; next if exists $role->{methods}{$method_name}; - next if $applicant_class_name->can($method_name); + next if $consumer_class_name->can($method_name); push @missing, $method_name; } @@ -80,8 +85,8 @@ sub _check_required_methods{ $role->throw_error(sprintf "'%s' requires the method%s %s to be implemented by '%s'", $role->name, (@missing == 1 ? '' : 's'), # method or methods - english_list(map{ sprintf q{'%s'}, $_ } @missing), - $applicant_class_name); + Mouse::Util::quoted_english_list(@missing), + $consumer_class_name); } } @@ -89,7 +94,7 @@ sub _check_required_methods{ } sub _apply_methods{ - my($role, $applicant, $args) = @_; + my($role, $consumer, $args) = @_; my $alias = $args->{-alias}; my $excludes = $args->{-excludes}; @@ -100,22 +105,22 @@ sub _apply_methods{ my $code = $role->get_method_body($method_name); if(!exists $excludes->{$method_name}){ - if(!$applicant->has_method($method_name)){ + if(!$consumer->has_method($method_name)){ # The third argument $role is used in Role::Composite - $applicant->add_method($method_name => $code, $role); + $consumer->add_method($method_name => $code, $role); } } if(exists $alias->{$method_name}){ my $dstname = $alias->{$method_name}; - my $dstcode = $applicant->get_method_body($dstname); + my $dstcode = $consumer->get_method_body($dstname); if(defined($dstcode) && $dstcode != $code){ $role->throw_error("Cannot create a method alias if a local method of the same name exists"); } else{ - $applicant->add_method($dstname => $code, $role); + $consumer->add_method($dstname => $code, $role); } } } @@ -124,35 +129,35 @@ sub _apply_methods{ } sub _apply_attributes{ - my($role, $applicant, $args) = @_; + my($role, $consumer, $args) = @_; for my $attr_name ($role->get_attribute_list) { - next if $applicant->has_attribute($attr_name); + next if $consumer->has_attribute($attr_name); - $applicant->add_attribute($attr_name => $role->get_attribute($attr_name)); + $consumer->add_attribute($attr_name => $role->get_attribute($attr_name)); } return; } sub _apply_modifiers{ - my($role, $applicant, $args) = @_; + my($role, $consumer, $args) = @_; if(my $modifiers = $role->{override_method_modifiers}){ foreach my $method_name (keys %{$modifiers}){ - $applicant->add_override_method_modifier($method_name => $modifiers->{$method_name}); + $consumer->add_override_method_modifier($method_name => $modifiers->{$method_name}); } } for my $modifier_type (qw/before around after/) { - my $modifiers = $role->{"${modifier_type}_method_modifiers"} + my $table = $role->{"${modifier_type}_method_modifiers"} or next; my $add_modifier = "add_${modifier_type}_method_modifier"; - foreach my $method_name (keys %{$modifiers}){ - foreach my $code(@{ $modifiers->{$method_name} }){ - next if $applicant->{"_applied_$modifier_type"}{$method_name, $code}++; # skip applied modifiers - $applicant->$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); } } } @@ -160,12 +165,12 @@ sub _apply_modifiers{ } sub _append_roles{ - my($role, $applicant, $args) = @_; + my($role, $consumer, $args) = @_; - my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles; + my $roles = $consumer->{roles}; foreach my $r($role, @{$role->get_roles}){ - if(!$applicant->does_role($r->name)){ + if(!$consumer->does_role($r)){ push @{$roles}, $r; } } @@ -174,24 +179,24 @@ sub _append_roles{ # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole sub apply { - my $self = shift; - my $applicant = shift; + my $self = shift; + my $consumer = shift; my %args = (@_ == 1) ? %{ $_[0] } : @_; my $instance; - if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass + if(Mouse::Util::is_a_metaclass($consumer)){ # Application::ToClass $args{_to} = 'class'; } - elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole + elsif(Mouse::Util::is_a_metarole($consumer)){ # Application::ToRole $args{_to} = 'role'; } else{ # Appplication::ToInstance $args{_to} = 'instance'; - $instance = $applicant; + $instance = $consumer; - $applicant = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class( + $consumer = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class( superclasses => [ref $instance], cache => 1, ); @@ -219,17 +224,17 @@ sub apply { } } - $self->_check_required_methods($applicant, \%args); - $self->_apply_attributes($applicant, \%args); - $self->_apply_methods($applicant, \%args); - $self->_apply_modifiers($applicant, \%args); - $self->_append_roles($applicant, \%args); + $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); if(defined $instance){ # Application::ToInstance # rebless instance - bless $instance, $applicant->name; - $applicant->_initialize_object($instance, $instance); + bless $instance, $consumer->name; + $consumer->_initialize_object($instance, $instance); } return; @@ -250,28 +255,13 @@ sub combine { return $composite; } -for my $modifier_type (qw/before after around/) { - - my $modifier = "${modifier_type}_method_modifiers"; - - my $add_method_modifier = sub { - my ($self, $method_name, $method) = @_; +sub add_before_method_modifier; +sub add_around_method_modifier; +sub add_after_method_modifier; - push @{ $self->{$modifier}->{$method_name} ||= [] }, $method; - return; - }; - - 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; - *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers; - - # has_${modifier_type}_method_modifiers is moved into t::lib::Test::Mouse -} +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) = @_; @@ -298,6 +288,8 @@ sub does_role { (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 @@ -314,6 +306,10 @@ __END__ Mouse::Meta::Role - The Mouse Role metaclass +=head1 VERSION + +This document describes Mouse version 0.50_03 + =head1 SEE ALSO L