complete attributeshortcuts support
[gitmo/Moo.git] / lib / Moo / HandleMoose.pm
index 5d5c522..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,12 +45,28 @@ 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);
     }
   }
   if ($am_role) {
-    $meta->add_required_methods(@{$Moo::Role::INFO{$name}{requires}});
+    my $info = $Moo::Role::INFO{$name};
+    $meta->add_required_methods(@{$info->{requires}});
+    foreach my $modifier (@{$info->{modifiers}}) {
+      my ($type, @args) = @$modifier;
+      $meta->${\"add_${type}_method_modifier"}(@args);
+    }
   } else {
     foreach my $attr (@attrs) {
       foreach my $method (@{$attr->associated_methods}) {
@@ -54,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;
 }