X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FRole.pm;h=fe6575ec3583d923d984d93d168faad4d5b83ecd;hb=823419c540f9e77090f31f11e04b14477c0372c4;hp=62382c4a81e6a2f745377fa0508622b6cf67f12e;hpb=71e7b544ac5b18ce7b09b9a95723b1a4e28b4321;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 62382c4..fe6575e 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); # 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 { $_[0]->{roles} } +sub get_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} }; @@ -56,180 +61,24 @@ sub add_attribute { my $name = shift; $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ }; -} - -sub _check_required_methods{ - my($role, $applicant, $args) = @_; - - if($args->{_to} eq 'role'){ - $applicant->add_required_methods($role->get_required_method_list); - } - else{ # to class or instance - my $class_name = $applicant->name; - my $role_name = $role->name; - my @missing; - foreach my $method_name(@{$role->{required_methods}}){ - if(!($class_name->can($method_name) || exists $args->{to_be_provided}{$method_name})){ - push @missing, $method_name; - } - } - if(@missing){ - $role->throw_error("'$role_name' requires the " - . (@missing == 1 ? 'method' : 'methods') - . " " - . english_list(map{ sprintf q{'%s'}, $_ } @missing) - . " to be implemented by '$class_name'"); - } - } - - return; -} - -sub _apply_methods{ - my($role, $applicant, $args) = @_; - - my $role_name = $role->name; - my $class_name = $applicant->name; - - my $alias = $args->{-alias}; - my $excludes = $args->{-excludes}; - - foreach my $method_name($role->get_method_list){ - next if $method_name eq 'meta'; - - my $code = $role->get_method_body($method_name); - - if($excludes && !exists $excludes->{$method_name}){ - if(!$applicant->has_method($method_name)){ - # The third argument, $role, is used in Role::Composite - $applicant->add_method($method_name => $code, $role); - } - } - - if($alias && $alias->{$method_name}){ - my $dstname = $alias->{$method_name}; - - my $dstcode = $applicant->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); - } - } - } - - return; -} - -sub _apply_attributes{ - my($role, $applicant, $args) = @_; - - for my $attr_name ($role->get_attribute_list) { - next if $applicant->has_attribute($attr_name); - - $applicant->add_attribute($attr_name => $role->get_attribute($attr_name)); - } - return; -} - -sub _apply_modifiers{ - my($role, $applicant, $args) = @_; - - for my $modifier_type (qw/override before around after/) { - my $modifiers = $role->{"${modifier_type}_method_modifiers"} - or next; - - my $add_modifier = "add_${modifier_type}_method_modifier"; - - foreach my $method_name (keys %{$modifiers}){ - my $modifier_codes = $modifiers->{$method_name}; - foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){ - next if $applicant->{"_$modifier_type"}{$code}++; # skip applied modifiers - $applicant->$add_modifier($method_name => $code); - } - } - } return; } -sub _append_roles{ - my($role, $applicant, $args) = @_; - - my $roles = ($args->{_to} eq 'role') ? $applicant->get_roles : $applicant->roles; - - foreach my $r($role, @{$role->get_roles}){ - if(!$applicant->does_role($r->name)){ - push @{$roles}, $r; - } - } - return; -} # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole sub apply { - my $self = shift; - my $applicant = shift; - - my %args = (@_ == 1) ? %{ $_[0] } : @_; - - if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass - $args{_to} = 'class'; - } - elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole - $args{_to} = 'role'; - } - else{ # Appplication::ToInstance - $args{_to} = 'instance'; - - my $metaclass = $applicant->meta->create_anon_class( - superclasses => [ref $applicant], - cache => 1, - ); - bless $applicant, $metaclass->name; # rebless - - $applicant = $metaclass; - } - - if($args{alias} && !exists $args{-alias}){ - $args{-alias} = $args{alias}; - } - if($args{excludes} && !exists $args{-excludes}){ - $args{-excludes} = $args{excludes}; - } - - # In Moose it is called 'aliased methods' - $args{to_be_provided} = {}; - if(my $alias = $args{-alias}){ - @{$args{to_be_provided}}{ values %{$alias} } = (); - } - @{ $args{to_be_provided} }{ $self->get_method_list } = (); - - 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; - } - } + my $self = shift; + my $consumer = shift; - $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); - return; + require 'Mouse/Meta/Role/Application.pm'; + return Mouse::Meta::Role::Application->new(@_)->apply($self, $consumer); } sub combine { - my($role_class, @role_specs) = @_; - - require 'Mouse/Meta/Role/Composite.pm'; # we don't want to create its namespace + my($self, @role_specs) = @_; + require 'Mouse/Meta/Role/Composite.pm'; my $composite = Mouse::Meta::Role::Composite->create_anon_role(); foreach my $role_spec (@role_specs) { @@ -239,30 +88,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) = @_; - - 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 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) = @_; @@ -278,29 +110,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 @@ -310,15 +132,22 @@ sub does_role { return 0; } - 1; - __END__ =head1 NAME Mouse::Meta::Role - The Mouse Role metaclass +=head1 VERSION + +This document describes Mouse version 0.70 + +=head1 DESCRIPTION + +This class is a meta object protocol for Mouse roles, +which is a subset of Moose::Meta:::Role. + =head1 SEE ALSO L