X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMethod%2FGenerate%2FConstructor.pm;h=33e6a080c559843bc759fd4579765f061ebaee5f;hb=160c2c2c5c944d61ada3e252d2e621f9074ebd9f;hp=43be5422916896ff9c82eba31b13d594bc7530f2;hpb=eae70e33b9265a0fdfa1ebda85dd68314e057783;p=gitmo%2FMoo.git diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index 43be542..33e6a08 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -42,9 +42,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 +61,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||{} @@ -88,9 +94,27 @@ sub _generate_args_via_buildargs { 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 {