X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMethod%2FGenerate%2FConstructor.pm;h=9190ea522482023b83f9d7469dd025cffc2d3fe8;hb=0123201bb23a0510ae9ad5817a5138fc2eb0cb3e;hp=236000614ef0fcb782c50a4fa880045d8f7ab9cb;hpb=b1eebd55fe3d34b6afa73a4880737dc91379b71e;p=gitmo%2FRole-Tiny.git diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index 2360006..9190ea5 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -20,6 +20,11 @@ sub accessor_generator { $_[0]->{accessor_generator} } +sub construction_string { + my ($self) = @_; + $self->{construction_string} or 'bless({}, $class);' +} + sub install_delayed { my ($self) = @_; my $package = $self->{package}; @@ -38,9 +43,15 @@ sub generate_method { } local $self->{captures} = {}; my $body = ' my $class = shift;'."\n"; - $body .= $self->_generate_args; + $body .= $self->_handle_subconstructor($into, $name); + 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; + } $body .= $self->_check_required($spec); - $body .= ' my $new = bless({}, $class);'."\n"; + $body .= ' my $new = '.$self->construction_string.";\n"; $body .= $self->_assign_new($spec); if ($into->can('BUILD')) { require Method::Generate::BuildAll; @@ -55,15 +66,50 @@ 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; $code; } +sub _generate_args_via_buildargs { + my ($self) = @_; + q{ my $args = $class->BUILDARGS(@_);}."\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 { @@ -72,11 +118,13 @@ sub _assign_new { my $ag = $self->accessor_generator; NAME: foreach my $name (sort keys %$spec) { my $attr_spec = $spec->{$name}; - next NAME unless defined(my $i = $attr_spec->{init_arg}); unless ($ag->is_simple_attribute($name, $attr_spec)) { - $test{$name} = $i; + next NAME unless defined($attr_spec->{init_arg}) + or $ag->has_eager_default($name, $attr_spec); + $test{$name} = $attr_spec->{init_arg}; next NAME; } + next NAME unless defined(my $i = $attr_spec->{init_arg}); push @init, $i; push @slots, $name; }