X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FRole.pm;h=01689cc3df83eb6ce2cb8197b7ceecf0fa0d4c1a;hb=50bc108bca305ced4df63a569c9b5f3474f71914;hp=2039b122a08b80dd313c0080cba80403c83794b2;hpb=a4e516f663c16c559013862e7f51b0aec4fc3e5b;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 2039b12..01689cc 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -5,11 +5,10 @@ use strict; use warnings; use metaclass; -use Sub::Name 'subname'; -use Carp 'confess'; -use Scalar::Util 'blessed', 'reftype'; +use Scalar::Util 'blessed'; -our $VERSION = '0.12'; +our $VERSION = '0.59'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use Moose::Meta::Class; @@ -122,6 +121,8 @@ foreach my $action ( sub add_attribute { my $self = shift; my $name = shift; + (defined $name && $name) + || Moose->throw_error("You must provide a name for the attribute"); my $attr_desc; if (scalar @_ == 1 && ref($_[0]) eq 'HASH') { $attr_desc = $_[0]; @@ -215,8 +216,8 @@ $META->add_attribute('override_method_modifiers' => ( sub add_override_method_modifier { my ($self, $method_name, $method) = @_; (!$self->has_method($method_name)) - || confess "Cannot add an override of method '$method_name' " . - "because there is a local version of '$method_name'"; + || Moose->throw_error("Cannot add an override of method '$method_name' " . + "because there is a local version of '$method_name'"); $self->get_override_method_modifiers_map->{$method_name} = $method; } @@ -241,6 +242,14 @@ sub get_method_modifier_list { keys %{$self->$accessor}; } +sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef } +sub update_package_cache_flag { + my $self = shift; + $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name); +} + + + ## ------------------------------------------------------------------ ## subroles @@ -252,8 +261,9 @@ __PACKAGE__->meta->add_attribute('roles' => ( sub add_role { my ($self, $role) = @_; (blessed($role) && $role->isa('Moose::Meta::Role')) - || confess "Roles must be instances of Moose::Meta::Role"; + || Moose->throw_error("Roles must be instances of Moose::Meta::Role"); push @{$self->get_roles} => $role; + $self->reset_package_cache_flag; } sub calculate_all_roles { @@ -269,7 +279,7 @@ sub calculate_all_roles { sub does_role { my ($self, $role_name) = @_; (defined $role_name) - || confess "You must supply a role name to look for"; + || Moose->throw_error("You must supply a role name to look for"); # if we are it,.. then return true return 1 if $role_name eq $self->name; # otherwise.. check our children @@ -286,14 +296,28 @@ sub method_metaclass { 'Moose::Meta::Role::Method' } sub get_method_map { my $self = shift; - my $map = {}; + + my $current = Class::MOP::check_package_cache_flag($self->name); + + if (defined $self->{'_package_cache_flag'} && $self->{'_package_cache_flag'} == $current) { + return $self->{'methods'} ||= {}; + } + + $self->{_package_cache_flag} = $current; + + my $map = $self->{'methods'} ||= {}; my $role_name = $self->name; my $method_metaclass = $self->method_metaclass; - foreach my $symbol ($self->list_all_package_symbols('CODE')) { + my %all_code = $self->get_all_package_symbols('CODE'); - my $code = $self->get_package_symbol('&' . $symbol); + foreach my $symbol (keys %all_code) { + my $code = $all_code{$symbol}; + + next if exists $map->{$symbol} && + defined $map->{$symbol} && + $map->{$symbol}->body == $code; my ($pkg, $name) = Class::MOP::get_code_info($code); @@ -305,17 +329,30 @@ sub get_method_map { # loudly (in the case of Curses.pm) so we # just be a little overly cautious here. # - SL - && eval { no warnings; blessed($pkg->meta) } + && eval { no warnings; blessed($pkg->meta) } # FIXME calls meta && $pkg->meta->isa('Moose::Meta::Role')) { my $role = $pkg->meta->name; next unless $self->does_role($role); } else { - next if ($pkg || '') ne $role_name && - ($name || '') ne '__ANON__'; + # NOTE: + # in 5.10 constant.pm the constants show up + # as being in the right package, but in pre-5.10 + # they show up as constant::__ANON__ so we + # make an exception here to be sure that things + # work as expected in both. + # - SL + unless ($pkg eq 'constant' && $name eq '__ANON__') { + next if ($pkg || '') ne $role_name || + (($name || '') ne '__ANON__' && ($pkg || '') ne $role_name); + } } - $map->{$symbol} = $method_metaclass->wrap($code); + $map->{$symbol} = $method_metaclass->wrap( + $code, + package_name => $role_name, + name => $name + ); } return $map; @@ -331,6 +368,53 @@ sub has_method { exists $self->get_method_map->{$name} ? 1 : 0 } +# FIXME this is copypasated from Class::MOP::Class +# refactor to inherit from some common base +sub wrap_method_body { + my ( $self, %args ) = @_; + + ('CODE' eq ref $args{body}) + || Moose->throw_error("Your code block must be a CODE reference"); + + $self->method_metaclass->wrap( + package_name => $self->name, + %args, + ); +} + +sub add_method { + my ($self, $method_name, $method) = @_; + (defined $method_name && $method_name) + || Moose->throw_error("You must define a method name"); + + my $body; + if (blessed($method)) { + $body = $method->body; + if ($method->package_name ne $self->name) { + $method = $method->clone( + package_name => $self->name, + name => $method_name + ) if $method->can('clone'); + } + } + else { + $body = $method; + $method = $self->wrap_method_body( body => $body, name => $method_name ); + } + + $method->attach_to_class($self); + + $self->get_method_map->{$method_name} = $method; + + my $full_method_name = ($self->name . '::' . $method_name); + $self->add_package_symbol( + { sigil => '&', type => 'CODE', name => $method_name }, + Class::MOP::subname($full_method_name => $body) + ); + + $self->update_package_cache_flag; # still valid, since we just added the method to the map, and if it was invalid before that then get_method_map updated it +} + sub find_method_by_name { (shift)->get_method(@_) } sub get_method_list { @@ -339,15 +423,9 @@ sub get_method_list { } sub alias_method { - my ($self, $method_name, $method) = @_; - (defined $method_name && $method_name) - || confess "You must define a method name"; - - my $body = (blessed($method) ? $method->body : $method); - ('CODE' eq (reftype($body) || '')) - || confess "Your code block must be a CODE reference"; + my $self = shift; - $self->add_package_symbol("&${method_name}" => $body); + $self->add_method(@_); } ## ------------------------------------------------------------------ @@ -358,7 +436,7 @@ sub apply { my ($self, $other, @args) = @_; (blessed($other)) - || confess "You must pass in an blessed instance"; + || Moose->throw_error("You must pass in an blessed instance"); if ($other->isa('Moose::Meta::Role')) { require Moose::Meta::Role::Application::ToRole; @@ -380,15 +458,19 @@ sub combine { require Moose::Meta::Role::Application::RoleSummation; require Moose::Meta::Role::Composite; - my @roles = map { $_->[0]->meta } @role_specs; - - my %params; - # how do I do this ... + my (@roles, %role_params); + while (@role_specs) { + my ($role, $params) = @{ splice @role_specs, 0, 1 }; + push @roles => $role->meta; + next unless defined $params; + $role_params{$role} = $params; + } my $c = Moose::Meta::Role::Composite->new(roles => \@roles); Moose::Meta::Role::Application::RoleSummation->new( - %params + role_params => \%role_params )->apply($c); + return $c; } @@ -540,6 +622,8 @@ probably not that much really). =item B +=item B + =item B =back @@ -588,6 +672,10 @@ probably not that much really). =item B +=item B + +=item B + =item B =item B