preserve attribute ordering
[gitmo/Moo.git] / lib / Moo / HandleMoose.pm
index 372bc12..e63e0be 100644 (file)
@@ -2,15 +2,21 @@ package Moo::HandleMoose;
 
 use strictures 1;
 use Moo::_Utils;
+use B qw(perlstring);
 
 our %TYPE_MAP;
 
-sub import { inject_all() }
+our $SETUP_DONE;
+
+sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; }
 
 sub inject_all {
   require Class::MOP;
-  inject_fake_metaclass_for($_) for grep $_ ne 'Moo::Object', keys %Moo::MAKERS;
+  inject_fake_metaclass_for($_)
+    for grep $_ ne 'Moo::Object', do { no warnings 'once'; keys %Moo::MAKERS };
   inject_fake_metaclass_for($_) for keys %Moo::Role::INFO;
+  require Moose::Meta::Method::Constructor;
+  @Moo::HandleMoose::FakeConstructor::ISA = 'Moose::Meta::Method::Constructor';
 }
 
 sub inject_fake_metaclass_for {
@@ -23,38 +29,67 @@ sub inject_fake_metaclass_for {
 
 our %DID_INJECT;
 
+{
+  package Moo::HandleMoose::FakeConstructor;
+
+  sub _uninlined_body { \&Moose::Object::new }
+}
+    
+
 sub inject_real_metaclass_for {
   my ($name) = @_;
   return Class::MOP::get_metaclass_by_name($name) if $DID_INJECT{$name};
   require Moose; require Moo; require Moo::Role;
   Class::MOP::remove_metaclass_by_name($name);
-  my ($am_role, $meta, $attr_specs) = do {
+  my ($am_role, $meta, $attr_specs, $attr_order) = do {
     if (my $info = $Moo::Role::INFO{$name}) {
-      (1, Moose::Meta::Role->initialize($name), $info->{attributes})
+      my @attr_info = @{$info->{attributes}||[]};
+      (1, Moose::Meta::Role->initialize($name),
+       { @attr_info },
+       [ @attr_info[grep !($_ % 2), 0..$#attr_info] ]
+      )
     } else {
       my $specs = Moo->_constructor_maker_for($name)->all_attribute_specs;
-      (0, Moose::Meta::Class->initialize($name), $specs);
+      (0, Moose::Meta::Class->initialize($name), $specs,
+       [ sort { $specs->{$a}{index} <=> $specs->{$b}{index} } keys %$specs ]
+      );
     }
   };
   my %methods = %{Role::Tiny->_concrete_methods_of($name)};
+  # needed to ensure the method body is stable and get things named
+  Sub::Defer::undefer_sub($_) for grep defined, values %methods;
   my @attrs;
   {
     # This local is completely not required for roles but harmless
     local @{_getstash($name)}{keys %methods};
-    foreach my $name (keys %$attr_specs) {
+    foreach my $name (@$attr_order) {
       my %spec = %{$attr_specs->{$name}};
-      $spec{is} = 'ro' if $spec{is} eq 'lazy';
+      delete $spec{index};
+      $spec{is} = 'ro' if $spec{is} eq 'lazy' or $spec{is} eq 'rwp';
+      delete $spec{asserter};
       if (my $isa = $spec{isa}) {
         $spec{isa} = do {
           if (my $mapped = $TYPE_MAP{$isa}) {
-            _load_module($mapped->[1]) if $mapped->[1];
-            Moose::Util::TypeConstraints::find_type_constraint($mapped->[0])
+            $mapped->();
           } else {
             Moose::Meta::TypeConstraint->new(
               constraint => sub { eval { &$isa; 1 } }
             );
           }
         };
+        die "Aaaargh" if $spec{coerce};
+      } elsif (my $coerce = $spec{coerce}) {
+        my $attr = perlstring($name);
+        my $tc = Moose::Meta::TypeConstraint->new(
+                   constraint => sub { die "This is not going to work" },
+                   inlined => sub {
+                      'my $r = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $r'
+                   },
+                 );
+         $tc->coercion(Moose::Meta::TypeCoercion->new)
+            ->_compiled_type_coercion($coerce);
+         $spec{isa} = $tc;
+         $spec{coerce} = 1;
       }
       push @attrs, $meta->add_attribute($name => %spec);
     }
@@ -72,7 +107,13 @@ sub inject_real_metaclass_for {
         $method->{body} = $name->can($method->name);
       }
     }
+    bless(
+      $meta->find_method_by_name('new'),
+      'Moo::HandleMoose::FakeConstructor',
+    );
   }
+  $meta->add_role(Class::MOP::class_of($_))
+    for do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} };
   $DID_INJECT{$name} = 1;
   $meta;
 }