more-tests
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
index f7a2252..7804b2a 100644 (file)
@@ -9,7 +9,7 @@ use Class::MOP;
 use Carp         'confess';
 use Scalar::Util 'weaken', 'blessed', 'reftype';
 
-our $VERSION = '0.05';
+our $VERSION = '0.06';
 
 use base 'Class::MOP::Class';
 
@@ -38,8 +38,22 @@ sub does_role {
     my ($self, $role_name) = @_;
     (defined $role_name)
         || confess "You must supply a role name to look for";
-    foreach my $role (@{$self->roles}) {
-        return 1 if $role->does_role($role_name);
+    foreach my $class ($self->class_precedence_list) {
+        foreach my $role (@{$class->meta->roles}) {
+            return 1 if $role->does_role($role_name);
+        }
+    }
+    return 0;
+}
+
+sub excludes_role {
+    my ($self, $role_name) = @_;
+    (defined $role_name)
+        || confess "You must supply a role name to look for";
+    foreach my $class ($self->class_precedence_list) {        
+        foreach my $role (@{$class->meta->roles}) {
+            return 1 if $role->excludes_role($role_name);
+        }
     }
     return 0;
 }
@@ -57,6 +71,10 @@ sub new_object {
 sub construct_instance {
     my ($class, %params) = @_;
     my $meta_instance = $class->get_meta_instance;
+    # FIXME:
+    # the code below is almost certainly incorrect
+    # but this is foreign inheritence, so we might
+    # have to kludge it in the end. 
     my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
     foreach my $attr ($class->compute_all_applicable_attributes()) {
         $attr->initialize_instance_slot($meta_instance, $instance, \%params)
@@ -79,103 +97,10 @@ sub has_method {
     return $self->SUPER::has_method($method_name);    
 }
 
-sub add_attribute {
-    my ($self, $name, %params) = @_;
-
-    my @delegations;
-    if ( my $delegation = delete $params{handles} ) {
-        my @method_names_or_hashes = $self->compute_delegation( $name, $delegation, \%params );
-        @delegations = $self->get_delegatable_methods( @method_names_or_hashes );
-    }
-
-    my $ret = $self->SUPER::add_attribute( $name, %params );
-
-    if ( @delegations ) {
-        my $attr = $self->get_attribute( $name );
-        $self->generate_delgate_method( $attr, $_ ) for @delegations;
-    }
-
-    return $ret;
-}
-
-sub generate_delgate_method {
-    my ( $self, $attr, $method ) = @_;
-
-    # FIXME like generated accessors these methods must be regenerated
-    # FIXME the reader may not work for subclasses with weird instances
-
-    my $reader = $attr->generate_reader_method( $attr->name ); # FIXME no need for attr name
-
-    my $method_name = $method->{name};
-    my $new_name = $method->{new_name} || $method_name;
-
-    $self->add_method( $new_name, sub {
-        if ( Scalar::Util::blessed( my $delegate = shift->$reader ) ) {
-            return $delegate->$method_name( @_ );
-        }
-        return;
-    });
-}
-
-sub compute_delegation {
-    my ( $self, $attr_name, $delegation, $params ) = @_;
-
-   
-    # either it's a concrete list of method names
-    return $delegation unless ref $delegation; # single method name
-    return @$delegation if reftype($delegation) eq "ARRAY";
-
-    # or it's a generative api
-    my $delegator_meta = $self->_guess_attr_class_or_role( $attr_name, $params );
-    $self->generate_delegation_list( $delegation, $delegator_meta );
-}
-
-sub get_delegatable_methods {
-    my ( $self, @names_or_hashes ) = @_;
-    my @hashes = map { ref($_) ? $_ : { name => $_ } } @names_or_hashes;
-    return grep { !$self->name->can( $_->{name} ) } @hashes;
-}
-
-sub generate_delegation_list {
-    my ( $self, $delegation, $delegator_meta ) = @_;
-
-    if ( reftype($delegation) eq "CODE" ) {
-        return $delegation->( $self, $delegator_meta );
-    } elsif ( blessed($delegation) eq "Regexp" ) {
-        confess "For regular expression support the delegator class/role must use a Class::MOP::Class metaclass"
-            unless $delegator_meta->isa( "Class::MOP::Class" );
-        return grep { $_->{name} =~ /$delegation/ } $delegator_meta->compute_all_applicable_methods();
-    } else {
-        confess "The 'handles' specification '$delegation' is not supported";
-    }
-}
-
-sub _guess_attr_class_or_role {
-    my ( $self, $attr, $params ) = @_;
-
-    my ( $isa, $does ) = @{ $params }{qw/isa does/};
-
-    confess "Generative delegations must explicitly specify a class or a role for the attribute's type"
-        unless $isa || $does;
-
-    for (grep { blessed($_) } $isa, $does) {
-        confess "You must use classes/roles, not type constraints to use delegation"
-            unless $_->isa( "Moose::Meta::Class" );
-    }
-    
-    confess "Cannot have an isa option and a does option if the isa does not do the does"
-        if $isa and $does and $isa->can("does") and !$isa->does( $does );
-
-    # if it's a class/role name make it into a meta object
-    for ($isa, $does) {
-        $_ = $_->meta if defined and !ref and $_->can("meta");
-    }
-
-    return $isa || $does;
-}
-
 sub add_override_method_modifier {
     my ($self, $name, $method, $_super_package) = @_;
+    (!$self->has_method($name))
+        || confess "Cannot add an override method if a local method is already present";
     # need this for roles ...
     $_super_package ||= $self->name;
     my $super = $self->find_next_method_by_name($name);
@@ -192,6 +117,8 @@ sub add_override_method_modifier {
 
 sub add_augment_method_modifier {
     my ($self, $name, $method) = @_;  
+    (!$self->has_method($name))
+        || confess "Cannot add an augment method if a local method is already present";    
     my $super = $self->find_next_method_by_name($name);
     (defined $super)
         || confess "You cannot augment '$name' because it has no super method";    
@@ -305,6 +232,33 @@ This will test if this class C<does> a given C<$role_name>. It will
 not only check it's local roles, but ask them as well in order to 
 cascade down the role hierarchy.
 
+=item B<excludes_role ($role_name)>
+
+This will test if this class C<excludes> a given C<$role_name>. It will 
+not only check it's local roles, but ask them as well in order to 
+cascade down the role hierarchy.
+
+=item B<add_attribute $attr_name, %params>
+
+This method does the same thing as L<Class::MOP::Class/add_attribute>, but adds
+suport for delegation.
+
+=back
+
+=head1 INTERNAL METHODS
+
+=over 4
+
+=item compute_delegation
+
+=item generate_delegation_list
+
+=item generate_delgate_method
+
+=item get_delegatable_methods
+
+=item filter_delegations
+
 =back
 
 =head1 BUGS
@@ -327,3 +281,4 @@ This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 
 
 =cut
+