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::_mro; |
7 | use Moo::_Utils; |
9aa500f6 |
8 | use B 'perlstring'; |
098a367b |
9 | |
10 | sub generate_method { |
11 | my ($self, $into) = @_; |
077bd026 |
12 | quote_sub "${into}::BUILDALL", join '', |
3c55a7ad |
13 | $self->_handle_subbuild($into), |
077bd026 |
14 | qq{ my \$self = shift;\n}, |
3c55a7ad |
15 | $self->buildall_body_for($into, '$self', '@_'), |
077bd026 |
16 | qq{ return \$self\n}; |
17 | } |
18 | |
06c4d037 |
19 | sub _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 |
26 | sub 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)}; |
f82d15d5 |
32 | join '', map qq{ ${me}->${_}(${args});\n}, @builds; |
098a367b |
33 | } |
34 | |
35 | 1; |