complete attributeshortcuts support
[gitmo/Moo.git] / lib / Moo / HandleMoose.pm
index 97b6c49..ed2e885 100644 (file)
@@ -3,7 +3,11 @@ package Moo::HandleMoose;
 use strictures 1;
 use Moo::_Utils;
 
-sub import { inject_all() }
+our %TYPE_MAP;
+
+our $SETUP_DONE;
+
+sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
 
 sub inject_all {
   require Class::MOP;
@@ -41,7 +45,18 @@ sub inject_real_metaclass_for {
     local @{_getstash($name)}{keys %methods};
     foreach my $name (keys %$attr_specs) {
       my %spec = %{$attr_specs->{$name}};
-      $spec{is} = 'ro' if $spec{is} eq 'lazy';
+      $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
+      if (my $isa = $spec{isa}) {
+        $spec{isa} = do {
+          if (my $mapped = $TYPE_MAP{$isa}) {
+            $mapped->();
+          } else {
+            Moose::Meta::TypeConstraint->new(
+              constraint => sub { eval { &$isa; 1 } }
+            );
+          }
+        };
+      }
       push @attrs, $meta->add_attribute($name => %spec);
     }
   }
@@ -59,6 +74,8 @@ sub inject_real_metaclass_for {
       }
     }
   }
+  $meta->add_role(Class::MOP::class_of($_))
+    for keys %{$Role::Tiny::APPLIED_TO{$name}};
   $DID_INJECT{$name} = 1;
   $meta;
 }