more readable subconstructor handling for BUILDALL
[gitmo/Role-Tiny.git] / lib / Method / Generate / BuildAll.pm
CommitLineData
098a367b 1package Method::Generate::BuildAll;
2
3use strictures 1;
b1eebd55 4use base qw(Moo::Object);
098a367b 5use Sub::Quote;
b1eebd55 6use Moo::_mro;
7use Moo::_Utils;
9aa500f6 8use B 'perlstring';
098a367b 9
10sub generate_method {
11 my ($self, $into) = @_;
077bd026 12 quote_sub "${into}::BUILDALL", join '',
06c4d037 13 $self->_handle_subbuild($into),
077bd026 14 qq{ my \$self = shift;\n},
06c4d037 15 $self->buildall_body_for($into, '$self', '@_'),
077bd026 16 qq{ return \$self\n};
17}
18
06c4d037 19sub _handle_subbuild {
20 my ($self, $into) = @_;
21 ' if (ref($_[0]) ne '.perlstring($into).') {'."\n".
22 ' return shift->Moo::Object::BUILDALL(@_)'.";\n".
23 ' }'."\n";
24}
25
077bd026 26sub buildall_body_for {
27 my ($self, $into, $me, $args) = @_;
098a367b 28 my @builds =
29 grep *{_getglob($_)}{CODE},
30 map "${_}::BUILD",
31 reverse @{mro::get_linear_isa($into)};
9aa500f6 32 join '', map qq{ ${me}->${_}(${args});\n}, @builds;
098a367b 33}
34
351;