Support Type in MMA::Native::Trait
Dave Rolsky [Sun, 30 Sep 2012 02:59:17 +0000 (21:59 -0500)]
lib/Moose/Meta/Attribute/Native/Trait.pm

index b006546..aac4c1d 100644 (file)
@@ -4,8 +4,9 @@ use Moose::Role;
 
 use Class::Load qw(load_class);
 use List::MoreUtils qw( any uniq );
-use Moose::Util::TypeConstraints;
 use Moose::Deprecated;
+use Moose::Util;
+use Moose::Util::TypeConstraints;
 
 requires '_helper_type';
 
@@ -87,12 +88,29 @@ sub _check_helper_type {
     $options->{isa} = $type
         unless exists $options->{isa};
 
-    my $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
-        $options->{isa} );
+    my $isa;
+    my $isa_name;
+
+    if (
+        Moose::Util::does_role(
+            $options->{isa}, 'Type::Constraint::Role::Interface'
+        )
+        ) {
+
+        $isa = $options->{isa};
+        require Type::Library::Builtins;
+        return if $isa->is_a_type_of( Type::Library::Builtins::t($type) );
+        $isa_name = $isa->name() || $isa->description();
+    }
+    else {
+        $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint(
+            $options->{isa} );
+        return if $isa->is_a_type_of($type);
+        $isa_name = $isa->name();
+    }
 
-    ( $isa->is_a_type_of($type) )
-        || confess
-        "The type constraint for $name must be a subtype of $type but it's a $isa";
+    confess
+        "The type constraint for $name must be a subtype of $type but it's a $isa_name";
 }
 
 before 'install_accessors' => sub { (shift)->_check_handles_values };