X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMethod%2FGenerate%2FConstructor.pm;h=707341dcb8ffd9e904ea8d9f31080f8fbd6f2d5f;hb=6b90ff030fd72db106f8f5e2151f49de310ab69d;hp=d62545d573a6259076bf1783f45c86fc7fa74ba0;hpb=649ac26439bb707eb25094d19ea056e2503356fe;p=gitmo%2FMoo.git diff --git a/lib/Method/Generate/Constructor.pm b/lib/Method/Generate/Constructor.pm index d62545d..707341d 100644 --- a/lib/Method/Generate/Constructor.pm +++ b/lib/Method/Generate/Constructor.pm @@ -2,7 +2,7 @@ package Method::Generate::Constructor; use strictures 1; use Sub::Quote; -use base qw(Class::Tiny::Object); +use base qw(Moo::Object); use Sub::Defer; use B 'perlstring'; @@ -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}; @@ -37,75 +42,111 @@ sub generate_method { $spec->{$no_init}{init_arg} = $no_init; } local $self->{captures} = {}; - my $body = ' my $class = shift;'."\n"; - $body .= $self->_generate_args; + my $body = ' my $class = shift;'."\n" + .' $class = ref($class) if ref($class);'."\n"; + $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 .= $self->_check_isa($spec); - $body .= ' my $new = bless({}, $class);'."\n"; + $body .= ' my $new = '.$self->construction_string.";\n"; $body .= $self->_assign_new($spec); - $body .= $self->_fire_triggers($spec); + if ($into->can('BUILD')) { + require Method::Generate::BuildAll; + $body .= Method::Generate::BuildAll->new->buildall_body_for( + $into, '$new', '$args' + ); + } $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||{} ; } +sub _handle_subconstructor { + my ($self, $into, $name) = @_; + if (my $gen = $self->{subconstructor_handler}) { + ' if ($class ne '.perlstring($into).') {'."\n". + $gen. + ' }'."\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 { my ($self, $spec) = @_; my (@init, @slots, %test); + 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}); - if ($attr_spec->{lazy} or $attr_spec->{default} or $attr_spec->{builder}) { - $test{$name} = $i; + unless ($ag->is_simple_attribute($name, $attr_spec)) { + 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; } return '' unless @init or %test; join '', ( @init - ? ' @{$new}{qw('.join(' ',@slots).')}' - .' = @{$args}{qw('.join(' ',@init).')};'."\n" + ? ' '.$self->_cap_call($ag->generate_multi_set( + '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}' + )).";\n" : '' ), map { my $arg_key = perlstring($test{$_}); - my $ag = $self->accessor_generator; my $test = "exists \$args->{$arg_key}"; my $source = "\$args->{$arg_key}"; my $attr_spec = $spec->{$_}; - my ($code, $add_captures); - if (!$attr_spec->{lazy} and - ($attr_spec->{default} or $attr_spec->{builder})) { - my $get_captures; - ($code, $add_captures) = $ag->generate_simple_set( - '$new', $_, - "(\n ${test}\n ? ${source}\n : " - .do { - (my $get, $get_captures) = $ag->generate_get_default( - '$new', $_, $attr_spec - ); - $get; - } - ."\n )" - ); - @{$add_captures}{keys %$get_captures} = values %$get_captures; - $code .= ";\n"; - } else { - ($code, $add_captures) = $ag->generate_simple_set( - '$new', $_, "\$args->{$arg_key}" - ); - $code .= " if ${test};\n"; - } - @{$self->{captures}}{keys %$add_captures} = values %$add_captures; - ' '.$code; + $self->_cap_call($ag->generate_populate_set( + '$new', $_, $attr_spec, $source, $test + )); } sort keys %test; }