File Coverage

blib/lib/MooX/Cmd/ChainedOptions.pm
Criterion Covered Total %
statement 35 35 100.0
branch 7 8 87.5
condition 2 2 100.0
subroutine 9 9 100.0
pod n/a
total 53 54 98.1


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
8             # and/or modify it under the terms of the GNU General Public License
9             # as published by the Free Software Foundation, either version 3 of
10             # the License, or (at 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;
23              
24 2     2   110230 use strict;
  2         3  
  2         54  
25 2     2   6 use warnings;
  2         3  
  2         56  
26              
27             our $VERSION = '0.03_02';
28              
29 2     2   377 use Import::Into;
  2         1844  
  2         33  
30 2     2   383 use Moo::Role ();
  2         11023  
  2         25  
31 2     2   881 use MooX::Options ();
  2         1649  
  2         28  
32              
33 2     2   620 use MooX::Cmd::ChainedOptions::Role ();
  2         3  
  2         39  
34 2     2   9 use List::Util qw/ first /;
  2         3  
  2         489  
35              
36             my %ROLE;
37              
38             sub import {
39              
40 7     7   47171 my $class = shift;
41 7         15 my $target = caller;
42              
43 7 100       36 unless ( $target->DOES( 'MooX::Cmd::Role' ) ) {
44 1         5 require Carp;
45 1         146 Carp::croak( "$target must use MooX::Cmd prior to using ",
46             __PACKAGE__, "\n" );
47             }
48              
49             # don't do this twice
50 6 50       120 return if $ROLE{$target};
51              
52             # load MooX::Options into target class.
53 6         29 MooX::Options->import::into( $target );
54              
55             # guess if an app or a command
56              
57             # if $target is a cmd, a parent class (app or cmd) must have
58             # been loaded. $target must be a direct descendant of a
59             # parent class' command_base. use the _build_command_base method
60             # as it can be used as a class method; command_base is an object method
61              
62 6         114995 my ( $base, $pkg ) = $target =~ /^(.*)?::([^:]+)$/;
63 6   100     27 $base ||= '';
64 6     9   46 my $parent = first { $base eq $_->_build_command_base } keys %ROLE;
  9         84  
65              
66 6 100       97 $ROLE{$target}
67             = $parent
68             ? MooX::Cmd::ChainedOptions::Role->build_variant( $parent,
69             $ROLE{$parent} )
70             : __PACKAGE__ . '::Base';
71              
72             # need only apply role to commands & subcommands
73 6 100       1701 Moo::Role->apply_roles_to_package( $target, $ROLE{$target} )
74             if $parent;
75              
76 6         2177 return;
77             }
78              
79             1;
80              
81              
82             __END__