lib/Moose/Meta/Attribute/Native/Trait.pm: factor out some of the namespace resolution...
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait.pm
index de59416..f03f509 100644 (file)
@@ -2,14 +2,11 @@
 package Moose::Meta::Attribute::Native::Trait;
 use Moose::Role;
 
+use Class::Load qw(load_class);
 use List::MoreUtils qw( any uniq );
 use Moose::Util::TypeConstraints;
 use Moose::Deprecated;
 
-our $VERSION   = '1.15';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
-
 requires '_helper_type';
 
 has _used_default_is => (
@@ -45,7 +42,7 @@ before '_process_options' => sub {
             feature => 'default default for Native Trait',
             message =>
                 'Allowing a native trait to automatically supply a default is deprecated.'
-                . ' You can avoid this warning by supply a default, builder, or making the attribute required'
+                . ' You can avoid this warning by supplying a default, builder, or making the attribute required'
         );
     }
 };
@@ -128,13 +125,24 @@ around '_canonicalize_handles' => sub {
             "The 'handles' option must be a HASH reference, not $handles");
     }
 
-    return map {
-        my $to = $handles->{$_};
-        $to = [$to] unless ref $to;
-        $_ => $to
-    } keys %$handles;
+    return
+        map { $_ => $self->_canonicalize_handles_value( $handles->{$_} ) }
+        keys %$handles;
 };
 
+sub _canonicalize_handles_value {
+    my $self  = shift;
+    my $value = shift;
+
+    if ( ref $value && 'ARRAY' ne ref $value ) {
+        $self->throw_error(
+            "All values passed to handles must be strings or ARRAY references, not $value"
+        );
+    }
+
+    return ref $value ? $value : [$value];
+}
+
 around '_make_delegation_method' => sub {
     my $next = shift;
     my ( $self, $handle_name, $method_to_call ) = @_;
@@ -161,14 +169,31 @@ sub _root_types {
     return $_[0]->_helper_type;
 }
 
+
+#
+# Foo::Bar::Baz::Quux::doo
+#
+# ^^^^^^^^^^^^^               - native accessor type prefix
+# ^^^^^^^^^^^^^^^^^^^         - native accessor method prefix
+# ^^^^^^^^^^^^^^^^^^^^^^^^    - native accessor methodclass for ( $suffix = doo )
+
+sub _native_accessor_type_prefix { 'Moose::Meta::Method::Accessor::Native' }
+
+sub _native_accessor_method_prefix {
+    my ( $self, ) = @_;
+    return $self->_native_accessor_type_prefix . '::' . $self->_native_type ;
+}
+sub _native_accessor_methodclass_for {
+    my ( $self, $suffix ) = @_;
+    return $self->_native_accessor_method_prefix . '::' . $suffix;
+}
+
 sub _native_accessor_class_for {
     my ( $self, $suffix ) = @_;
 
-    my $role
-        = 'Moose::Meta::Method::Accessor::Native::'
-        . $self->_native_type . '::'
-        . $suffix;
+    my $role = $self->_native_accessor_methodclass_for( $suffix );
 
+    load_class($role);
     return Moose::Meta::Class->create_anon_class(
         superclasses =>
             [ $self->accessor_metaclass, $self->delegation_metaclass ],
@@ -177,11 +202,15 @@ sub _native_accessor_class_for {
     )->name;
 }
 
+sub _native_type_matcher { qr/::Native::Trait::(\w+)$/ }
+
 sub _build_native_type {
     my $self = shift;
+    my $matcher = $self->_native_type_matcher;
 
     for my $role_name ( map { $_->name } $self->meta->calculate_all_roles ) {
-        return $1 if $role_name =~ /::Native::Trait::(\w+)$/;
+
+        return $1 if $role_name =~ $matcher
     }
 
     die "Cannot calculate native type for " . ref $self;
@@ -199,11 +228,9 @@ no Moose::Util::TypeConstraints;
 
 1;
 
-__END__
+# ABSTRACT: Shared role for native delegation traits
 
-=head1 NAME
-
-Moose::Meta::Attribute::Native::Trait - Shared role for native delegation traits
+__END__
 
 =head1 BUGS
 
@@ -214,21 +241,4 @@ See L<Moose/BUGS> for details on reporting bugs.
 Documentation for Moose native traits can be found in
 L<Moose::Meta::Attribute::Native>.
 
-=head1 AUTHORS
-
-Yuval Kogman
-
-Shawn M Moore
-
-Jesse Luehrs
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2007-2009 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
 =cut