update repo to point to github
[gitmo/Moo.git] / lib / Method / Generate / Constructor.pm
index 50a5659..c116eff 100644 (file)
@@ -5,6 +5,7 @@ use Sub::Quote;
 use base qw(Moo::Object);
 use Sub::Defer;
 use B 'perlstring';
+use Moo::_Utils qw(_getstash);
 
 sub register_attribute_specs {
   my ($self, @new_specs) = @_;
@@ -14,8 +15,16 @@ sub register_attribute_specs {
       die "has '+${name}' given but no ${name} attribute already exists"
         unless my $old_spec = $specs->{$name};
       foreach my $key (keys %$old_spec) {
-        $new_spec->{$key} = $old_spec->{$key}
-          unless exists $new_spec->{$key};
+        if (!exists $new_spec->{$key}) {
+          $new_spec->{$key} = $old_spec->{$key}
+            unless $key eq 'handles';
+        }
+        elsif ($key eq 'moosify') {
+          $new_spec->{$key} = [
+            map { ref $_ eq 'ARRAY' ? @$_ : $_ }
+              ($old_spec->{$key}, $new_spec->{$key})
+          ];
+        }
       }
     }
     $new_spec->{index} = scalar keys %$specs
@@ -36,9 +45,13 @@ sub accessor_generator {
 sub construction_string {
   my ($self) = @_;
   $self->{construction_string}
-    or 'bless('
-       .$self->accessor_generator->default_construction_string
-       .', $class);'
+    ||= $self->_build_construction_string;
+}
+
+sub _build_construction_string {
+  'bless('
+    .$_[0]->accessor_generator->default_construction_string
+    .', $class);'
 }
 
 sub install_delayed {
@@ -170,4 +183,16 @@ sub _check_required {
     ."    }\n";
 }
 
+use Moo;
+Moo->_constructor_maker_for(__PACKAGE__)->register_attribute_specs(
+  attribute_specs => {
+    is => 'ro',
+    reader => 'all_attribute_specs',
+  },
+  accessor_generator => { is => 'ro' },
+  construction_string => { is => 'lazy' },
+  subconstructor_handler => { is => 'ro' },
+  package => { is => 'ro' },
+);
+
 1;