added BUILDARGS support
[gitmo/Role-Tiny.git] / lib / Method / Generate / Constructor.pm
index 5d459b6..23d9c03 100644 (file)
@@ -43,6 +43,7 @@ sub generate_method {
   }
   local $self->{captures} = {};
   my $body = '    my $class = shift;'."\n";
+  $body .= $self->_handle_subconstructor($into, $name);
   $body .= $self->_generate_args;
   $body .= $self->_check_required($spec);
   $body .= '    my $new = '.$self->construction_string.";\n";
@@ -60,6 +61,18 @@ sub generate_method {
   ;
 }
 
+sub _handle_subconstructor {
+  my ($self, $into, $name) = @_;
+  if (my $gen = $self->{subconstructor_generator}) {
+    '    if ($class ne '.perlstring($into).') {'."\n".
+    '      '.$gen.";\n".
+    '      return $class->'.$name.'(@_)'.";\n".
+    '    }'."\n";
+  } else {
+    ''
+  }
+}
+
 sub _cap_call {
   my ($self, $code, $captures) = @_;
   @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
@@ -68,7 +81,7 @@ sub _cap_call {
 
 sub _generate_args {
   my ($self) = @_;
-  q{    my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
+  q{    my $args = $class->BUILDARGS(@_);}."\n";
 }
 
 sub _assign_new {
@@ -79,8 +92,7 @@ sub _assign_new {
     my $attr_spec = $spec->{$name};
     unless ($ag->is_simple_attribute($name, $attr_spec)) {
       next NAME unless defined($attr_spec->{init_arg})
-                         or (($attr_spec->{default} or $attr_spec->{builder})
-                             and not $attr_spec->{lazy});
+                         or $ag->has_eager_default($name, $attr_spec);
       $test{$name} = $attr_spec->{init_arg};
       next NAME;
     }