more multiple inheritance fiddling
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
index 02b0400..1dcf13c 100644 (file)
@@ -9,29 +9,6 @@ our $AUTHORITY = 'cpan:STEVAN';
 
 requires '_helper_type';
 
-# these next two are the possible methods you can use in the 'handles'
-# map.
-
-# provide a Class or Role which we can collect the method providers
-# from
-
-# or you can provide a HASH ref of anon subs yourself. This will also
-# collect and store the methods from a method_provider as well
-has 'method_constructors' => (
-    is      => 'ro',
-    isa     => 'HashRef',
-    lazy    => 1,
-    default => sub {
-        my $self = shift;
-        return +{} unless $self->has_method_provider;
-
-        # or grab them from the role/class
-        my $method_provider = $self->method_provider->meta;
-        return +{ map { $_->name => $_ }
-                $method_provider->_get_local_methods };
-    },
-);
-
 before '_process_options' => sub {
     my ( $self, $name, $options ) = @_;
 
@@ -65,16 +42,17 @@ before 'install_accessors' => sub { (shift)->_check_handles_values };
 sub _check_handles_values {
     my $self = shift;
 
-    my $method_constructors = $self->method_constructors;
-
     my %handles = $self->_canonicalize_handles;
 
     for my $original_method ( values %handles ) {
         my $name = $original_method->[0];
-        ( exists $method_constructors->{$name} )
-            || confess "$name is an unsupported method type";
-    }
 
+        my $accessor_class = $self->_native_accessor_class_for($name);
+
+        ( $accessor_class && $accessor_class->can('new') )
+            || confess
+            "$name is an unsupported method type - $accessor_class";
+    }
 }
 
 around '_canonicalize_handles' => sub {
@@ -102,24 +80,59 @@ around '_make_delegation_method' => sub {
 
     my ( $name, @curried_args ) = @$method_to_call;
 
-    my $method_constructors = $self->method_constructors;
+    my $accessor_class = $self->_native_accessor_class_for($name);
 
-    my $code = $method_constructors->{$name}->(
-        $self,
-        $self->get_read_method_ref,
-        $self->get_write_method_ref,
-    );
+    die "Cannot find an accessor class for $name"
+        unless $accessor_class && $accessor_class->can('new');
 
-    return $next->(
-        $self,
-        $handle_name,
-        sub {
-            my $instance = shift;
-            return $code->( $instance, @curried_args, @_ );
-        },
+    return $accessor_class->new(
+        name               => $handle_name,
+        package_name       => $self->associated_class->name,
+        delegate_to_method => $name,
+        attribute          => $self,
+        is_inline          => 1,
+        curried_arguments  => \@curried_args,
+        root_types         => [ $self->_root_types ],
     );
 };
 
+sub _root_types {
+    return $_[0]->_helper_type;
+}
+
+sub _native_accessor_class_for {
+    my ( $self, $suffix ) = @_;
+
+    my $role
+        = 'Moose::Meta::Method::Accessor::Native::'
+        . $self->_native_type . '::'
+        . $suffix;
+
+    return Moose::Meta::Class->create_anon_class(
+        superclasses =>
+            [ $self->accessor_metaclass, $self->delegation_metaclass ],
+        roles => [$role],
+        cache => 1,
+    )->name;
+}
+
+sub _build_native_type {
+    my $self = shift;
+
+    for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
+        return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
+    }
+
+    die "Cannot calculate native type for " . ref $self;
+}
+
+has '_native_type' => (
+    is      => 'ro',
+    isa     => 'Str',
+    lazy    => 1,
+    builder => '_build_native_type',
+);
+
 no Moose::Role;
 no Moose::Util::TypeConstraints;