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 '', |
13 | qq{ my \$self = shift;\n}, |
9aa500f6 |
14 | qq{ my \$class = ref \$self;\n}, |
15 | ' if ('. perlstring($into) ." ne \$class) {\n", |
16 | qq{ return \$self->\${\\(\$Moo::Object::BUILD_MAKER->generate_method(\$class))}(\@_);\n}, |
17 | " } else {\n", |
18 | $self->buildall_body_for($into, '$self', '@_'), |
19 | " }\n", |
077bd026 |
20 | qq{ return \$self\n}; |
21 | } |
22 | |
23 | sub buildall_body_for { |
24 | my ($self, $into, $me, $args) = @_; |
098a367b |
25 | my @builds = |
26 | grep *{_getglob($_)}{CODE}, |
27 | map "${_}::BUILD", |
28 | reverse @{mro::get_linear_isa($into)}; |
9aa500f6 |
29 | join '', map qq{ ${me}->${_}(${args});\n}, @builds; |
098a367b |
30 | } |
31 | |
32 | 1; |