clean up coerce generation a bit
[gitmo/Role-Tiny.git] / lib / Method / Generate / BuildAll.pm
1 package Method::Generate::BuildAll;
2
3 use strictures 1;
4 use base qw(Moo::Object);
5 use Sub::Quote;
6 use Moo::_Utils;
7 use B 'perlstring';
8
9 sub generate_method {
10   my ($self, $into) = @_;
11   quote_sub "${into}::BUILDALL", join '',
12     $self->_handle_subbuild($into),
13     qq{    my \$self = shift;\n},
14     $self->buildall_body_for($into, '$self', '@_'),
15     qq{    return \$self\n};
16 }
17
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
25 sub buildall_body_for {
26   my ($self, $into, $me, $args) = @_;
27   my @builds =
28     grep *{_getglob($_)}{CODE},
29     map "${_}::BUILD",
30     reverse @{Moo::_Utils::_get_linear_isa($into)};
31   join '', map qq{    ${me}->${_}(${args});\n}, @builds;
32 }
33
34 1;