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=4a36f243be9fc0b222daa15657ad3fb5752e96a4;hpb=e4be029763d52eb7f1a446c4f262e1f770492743;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Role.pm b/lib/Moose/Meta/Role.pm index 4a36f24..01689cc 100644 --- a/lib/Moose/Meta/Role.pm +++ b/lib/Moose/Meta/Role.pm @@ -5,10 +5,10 @@ use strict; use warnings; use metaclass; -use Carp 'confess'; use Scalar::Util 'blessed'; -our $VERSION = '0.56'; +our $VERSION = '0.59'; +$VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; use Moose::Meta::Class; @@ -121,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]; @@ -214,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; } @@ -259,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 { @@ -276,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 @@ -367,36 +370,40 @@ sub has_method { # 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) - || confess "You must define a 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->name ne $method_name) { - warn "Hello there, got something for you." - . " Method says " . $method->package_name . " " . $method->name - . " Class says " . $self->name . " " . $method_name; + if ($method->package_name ne $self->name) { $method = $method->clone( package_name => $self->name, - name => $method_name + name => $method_name ) if $method->can('clone'); } } else { $body = $method; - ('CODE' eq ref($body)) - || confess "Your code block must be a CODE reference"; - $method = $self->method_metaclass->wrap( - $body => ( - package_name => $self->name, - name => $method_name - ) - ); + $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); @@ -404,7 +411,8 @@ sub add_method { { sigil => '&', type => 'CODE', name => $method_name }, Class::MOP::subname($full_method_name => $body) ); - $self->update_package_cache_flag; + + $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(@_) } @@ -415,18 +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 ref($body)) - || confess "Your code block must be a CODE reference"; + my $self = shift; - $self->add_package_symbol( - { sigil => '&', type => 'CODE', name => $method_name }, - $body - ); + $self->add_method(@_); } ## ------------------------------------------------------------------ @@ -437,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; @@ -453,16 +452,6 @@ sub apply { } } -sub apply_to_metaclass_instance { - my ($self, $meta, @args) = @_; - - $meta->isa('Moose::Meta::Class') || $meta->isa('Moose::Meta::Role') - || confess "You must pass in a Moose::Meta::Class or Moose::Meta::Role instance"; - - require Moose::Meta::Role::Application::ToMetaclassInstance; - return Moose::Meta::Role::Application::ToMetaclassInstance->new(@args)->apply($self, $meta); -} - sub combine { my ($class, @role_specs) = @_; @@ -685,6 +674,8 @@ probably not that much really). =item B +=item B + =item B =item B