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