preserve attribute ordering
[gitmo/Moo.git] / lib / Method / Generate / Constructor.pm
index 43be542..1aa8225 100644 (file)
@@ -7,8 +7,13 @@ use Sub::Defer;
 use B 'perlstring';
 
 sub register_attribute_specs {
-  my ($self, %spec) = @_;
-  @{$self->{attribute_specs}||={}}{keys %spec} = values %spec;
+  my ($self, @new_specs) = @_;
+  my $specs = $self->{attribute_specs}||={};
+  while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
+    $new_spec->{index} = scalar keys %$specs
+      unless defined $new_spec->{index};
+    $specs->{$name} = $new_spec;
+  }
   $self;
 }
 
@@ -22,7 +27,10 @@ sub accessor_generator {
 
 sub construction_string {
   my ($self) = @_;
-  $self->{construction_string} or 'bless({}, $class);'
+  $self->{construction_string}
+    or 'bless('
+       .$self->accessor_generator->default_construction_string
+       .', $class);'
 }
 
 sub install_delayed {
@@ -42,9 +50,11 @@ sub generate_method {
     $spec->{$no_init}{init_arg} = $no_init;
   }
   local $self->{captures} = {};
-  my $body = '    my $class = shift;'."\n";
+  my $body = '    my $class = shift;'."\n"
+            .'    $class = ref($class) if ref($class);'."\n";
   $body .= $self->_handle_subconstructor($into, $name);
-  if ($into->can('BUILDARGS') ) {
+  my $into_buildargs = $into->can('BUILDARGS');
+  if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
       $body .= $self->_generate_args_via_buildargs;
   } else {
       $body .= $self->_generate_args;
@@ -59,6 +69,10 @@ sub generate_method {
     );
   }
   $body .= '    return $new;'."\n";
+  if ($into->can('DEMOLISH')) {
+    require Method::Generate::DemolishAll;
+    Method::Generate::DemolishAll->new->generate_method($into);
+  }
   quote_sub
     "${into}::${name}" => $body,
     $self->{captures}, $quote_opts||{}
@@ -67,10 +81,9 @@ sub generate_method {
 
 sub _handle_subconstructor {
   my ($self, $into, $name) = @_;
-  if (my $gen = $self->{subconstructor_generator}) {
+  if (my $gen = $self->{subconstructor_handler}) {
     '    if ($class ne '.perlstring($into).') {'."\n".
-    '      '.$gen.";\n".
-    '      return $class->'.$name.'(@_)'.";\n".
+    $gen.
     '    }'."\n";
   } else {
     ''
@@ -85,12 +98,32 @@ sub _cap_call {
 
 sub _generate_args_via_buildargs {
   my ($self) = @_;
-  q{    my $args = $class->BUILDARGS(@_);}."\n";
+  q{    my $args = $class->BUILDARGS(@_);}."\n"
+  .q{    die "BUILDARGS did not return a hashref" unless ref($args) eq 'HASH';}
+  ."\n";
 }
 
+# inlined from Moo::Object - update that first.
 sub _generate_args {
   my ($self) = @_;
-  q{    my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
+  return <<'_EOA';
+    my $args;
+    if ( scalar @_ == 1 ) {
+        unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
+            die "Single parameters to new() must be a HASH ref"
+                ." data => ". $_[0] ."\n";
+        }
+        $args = { %{ $_[0] } };
+    }
+    elsif ( @_ % 2 ) {
+        die "The new() method for $class expects a hash reference or a key/value list."
+                . " You passed an odd number of arguments\n";
+    }
+    else {
+        $args = {@_};
+    }
+_EOA
+
 }
 
 sub _assign_new {