File Coverage

blib/lib/MooX/Cmd/ChainedOptions/Base.pm
Criterion Covered Total %
statement 15 15 100.0
branch 4 4 100.0
condition n/a
subroutine 3 3 100.0
pod n/a
total 22 22 100.0


line stmt bran cond sub pod time code
1             # --8<--8<--8<--8<--
2             #
3             # Copyright (C) 2015 Smithsonian Astrophysical Observatory
4             #
5             # This file is part of MooX-Cmd-ChainedOptions
6             #
7             # MooX-Cmd-ChainedOptions is free software: you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation, either version 3 of the License, or (at
10             # your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program. If not, see .
19             #
20             # -->8-->8-->8-->8--
21              
22             package MooX::Cmd::ChainedOptions::Base;
23              
24 2     2   1576 use Moo::Role;
  2         3  
  2         8  
25 2     2   493 use Scalar::Util qw[ blessed ];
  2         3  
  2         284  
26              
27             has _parent => (
28             is => 'lazy',
29             init_arg => undef,
30             builder => sub {
31              
32 13     13   64829 my $class = blessed $_[0];
33              
34             # Find the element in command chain array which directly
35             # precedes the element containing the current class.
36              
37             # There is a single array used for the command chain, and it
38             # is populated by MooX::Cmd as it processes the command line.
39             # This builder may be called for a class after MooX::Cmd has
40             # added entries for the class' subcommands, so we can't simply
41             # assume that the last element in the array is for this class.
42              
43 13         20 my $last;
44 13         15 for ( reverse @{ $_[0]->command_chain } ) {
  13         46  
45 28 100       44 next unless $last;
46 16 100       201 return $_ if blessed $last eq $class;
47             }
48 16         17 continue { $last = $_ }
49              
50 1         6 require Carp;
51 1         100 Carp::croak( "unable to determine parent in chain hierarchy\n" );
52             },
53             );
54              
55             1;
56              
57             __END__