Tenative switch to a generated DEMOLISHALL - see rest of message for caveats
[gitmo/Moo.git] / lib / Method / Generate / DemolishAll.pm
CommitLineData
56ffe19d 1package Method::Generate::DemolishAll;
2
3use strictures 1;
4use base qw(Moo::Object);
5use Sub::Quote;
6use Moo::_Utils;
7use B qw(perlstring);
8
9sub generate_method {
10 my ($self, $into) = @_;
11 quote_sub "${into}::DEMOLISHALL", join '',
12 $self->_handle_subdemolish($into),
13 qq{ my \$self = shift;\n},
14 $self->demolishall_body_for($into, '$self', '@_'),
15 qq{ return \$self\n};
16}
17
18sub demolishall_body_for {
19 my ($self, $into, $me, $args) = @_;
20 my @demolishers =
21 grep *{_getglob($_)}{CODE},
22 map "${_}::DEMOLISH",
23 @{Moo::_Utils::_get_linear_isa($into)};
24 join '', map qq{ ${me}->${_}(${args});\n}, @demolishers;
25}
26
27sub _handle_subdemolish {
28 my ($self, $into) = @_;
29 ' if (ref($_[0]) ne '.perlstring($into).') {'."\n".
30 ' return shift->Moo::Object::DEMOLISHALL(@_)'.";\n".
31 ' }'."\n";
32}
33
341;