apply coercion to subtype of inflated moose type when using %TYPE_MAP
Graham Knop [Fri, 22 Feb 2013 23:20:09 +0000 (18:20 -0500)]
lib/Moo/HandleMoose.pm

index cb2c2a2..c7c29f0 100644 (file)
@@ -99,22 +99,24 @@ sub inject_real_metaclass_for {
       delete $spec{index};
       $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
       delete $spec{asserter};
+      my $coerce = $spec{coerce};
       if (my $isa = $spec{isa}) {
         my $tc = $spec{isa} = do {
           if (my $mapped = $TYPE_MAP{$isa}) {
-            $mapped->();
+            my $type = $mapped->();
+            $coerce ? $type->create_child_type(name => $type->name) : $type;
           } else {
             Moose::Meta::TypeConstraint->new(
               constraint => sub { eval { &$isa; 1 } }
             );
           }
         };
-        if (my $coerce = $spec{coerce}) {
+        if ($coerce) {
           $tc->coercion(Moose::Meta::TypeCoercion->new)
              ->_compiled_type_coercion($coerce);
           $spec{coerce} = 1;
         }
-      } elsif (my $coerce = $spec{coerce}) {
+      } elsif ($coerce) {
         my $attr = perlstring($name);
         my $tc = Moose::Meta::TypeConstraint->new(
                    constraint => sub { die "This is not going to work" },