Mouse::Role improved
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
index 403b781..05faacf 100644 (file)
@@ -1,31 +1,11 @@
 package Mouse::Meta::Role;
 use strict;
 use warnings;
-use Carp 'confess';
 
+use Mouse::Util qw(not_supported);
 use base qw(Mouse::Meta::Module);
 
-do {
-    my %METACLASS_CACHE;
-
-    # because Mouse doesn't introspect existing classes, we're forced to
-    # only pay attention to other Mouse classes
-    sub _metaclass_cache {
-        my $class = shift;
-        my $name  = shift;
-        return $METACLASS_CACHE{$name};
-    }
-
-    sub initialize {
-        my($class, $package_name, @args) = @_;
-
-        ($package_name && !ref($package_name))\r
-            || confess("You must pass a package name and it cannot be blessed");\r
-
-        return $METACLASS_CACHE{$package_name}
-            ||= $class->_new(package => $package_name, @args);
-    }
-};
+sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
 
 sub _new {
     my $class = shift;
@@ -41,6 +21,9 @@ sub _new {
 
 sub get_roles { $_[0]->{roles} }
 
+sub get_required_method_list{
+    return @{ $_[0]->{required_methods} };
+}
 
 sub add_required_methods {
     my $self = shift;
@@ -48,16 +31,17 @@ sub add_required_methods {
     push @{$self->{required_methods}}, @methods;
 }
 
+sub requires_method {
+    my($self, $name) = @_;
+    return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
+}
+
 sub add_attribute {
     my $self = shift;
     my $name = shift;
-    my $spec = shift;
-    $self->{attributes}->{$name} = $spec;
-}
 
-sub has_attribute { exists $_[0]->{attributes}->{$_[1]}  }
-sub get_attribute_list { keys %{ $_[0]->{attributes} } }
-sub get_attribute { $_[0]->{attributes}->{$_[1]} }
+    $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
+}
 
 sub _check_required_methods{
     my($role, $class, $args, @other_roles) = @_;
@@ -77,7 +61,7 @@ sub _check_required_methods{
                     }
                 }
                 
-                confess "'$role_name' requires the method '$method_name' to be implemented by '$class_name'"
+                $role->throw_error("'$role_name' requires the method '$method_name' to be implemented by '$class_name'")
                     unless $has_method;
             }
         }
@@ -91,23 +75,38 @@ sub _apply_methods{
 
     my $role_name  = $role->name;
     my $class_name = $class->name;
-    my $alias      = $args->{alias};
+
+    my $alias    = (exists $args->{alias}    && !exists $args->{-alias})    ? $args->{alias}    : $args->{-alias};
+    my $excludes = (exists $args->{excludes} && !exists $args->{-excludes}) ? $args->{excludes} : $args->{-excludes};
+
+    my %exclude_map;
+
+    if(defined $excludes){
+        if(ref $excludes){
+            %exclude_map = map{ $_ => undef } @{$excludes};
+        }
+        else{
+            $exclude_map{$excludes} = undef;
+        }
+    }
 
     foreach my $method_name($role->get_method_list){
         next if $method_name eq 'meta';
 
         my $code = $role_name->can($method_name);
-        if(do{ no strict 'refs'; defined &{$class_name . '::' . $method_name} }){
-            # XXX what's Moose's behavior?
-        }
-        else{
-            $class->add_method($method_name => $code);
+
+        if(!exists $exclude_map{$method_name}){
+            if(!$class->has_method($method_name)){
+                $class->add_method($method_name => $code);
+            }
         }
 
         if($alias && $alias->{$method_name}){
             my $dstname = $alias->{$method_name};
-            if(do{ no strict 'refs'; defined &{$class_name . '::' . $dstname} }){
-                # XXX wat's Moose's behavior?
+
+            my $slot = do{ no strict 'refs'; \*{$class_name . '::' . $dstname} };
+            if(defined(*{$slot}{CODE}) && *{$slot}{CODE} != $code){
+                $class->throw_error("Cannot create a method alias if a local method of the same name exists");
             }
             else{
                 $class->add_method($dstname => $code);
@@ -159,7 +158,7 @@ sub _apply_modifiers{
         my $modifiers    = $role->{"${modifier_type}_method_modifiers"};
 
         while(my($method_name, $modifier_codes) = each %{$modifiers}){
-            foreach my $code(@{$modifier_codes}){
+            foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
                 $class->$add_modifier($method_name => $code);
             }
         }
@@ -185,7 +184,7 @@ sub apply {
     my($self, $class, %args) = @_;
 
     if ($class->isa('Mouse::Object')) {
-        Carp::croak('Mouse does not support Application::ToInstance yet');
+        not_supported 'Application::ToInstance';
     }
 
     $self->_check_required_methods($class, \%args);
@@ -213,7 +212,7 @@ sub combine_apply {
     return;
 }
 
-for my $modifier_type (qw/before after around override/) {
+for my $modifier_type (qw/before after around/) {
 
     my $modifier = "${modifier_type}_method_modifiers";
     my $add_method_modifier =  sub {
@@ -222,6 +221,11 @@ for my $modifier_type (qw/before after around override/) {
         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} ||= [] }
@@ -229,15 +233,42 @@ for my $modifier_type (qw/before after around override/) {
 
     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_override_method_modifier{
+    my($self, $method_name, $method) = @_;
+
+    (!$self->has_method($method_name))\r
+        || $self->throw_error("Cannot add an override of method '$method_name' " .\r
+                   "because there is a local version of '$method_name'");
+
+    $self->{override_method_modifiers}->{$method_name} = $method;
+}
+
+sub has_override_method_modifier {\r
+    my ($self, $method_name) = @_;\r
+    return exists $self->{override_method_modifiers}->{$method_name};\r
+}\r
+\r
+sub get_override_method_modifier {\r
+    my ($self, $method_name) = @_;\r
+    return $self->{override_method_modifiers}->{$method_name};\r
+}
+
+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)
-        || confess "You must supply a role name to look for";
+        || $self->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;