File Coverage

blib/lib/YA/CLI/ActionRole.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 10 100.0
condition n/a
subroutine 13 13 100.0
pod 4 6 66.6
total 66 68 97.0


line stmt bran cond sub pod time code
1             package YA::CLI::ActionRole;
2             our $VERSION = '0.007';
3 4     4   230200 use Moo::Role;
  4         11601  
  4         29  
4 4     4   3041 use namespace::autoclean;
  4         15368  
  4         71  
5              
6             # ABSTRACT: Action handler role
7              
8 4     4   1236 use YA::CLI::Usage;
  4         1763  
  4         158  
9 4     4   1255 use Getopt::Long;
  4         20242  
  4         32  
10 4     4   680 use List::Util qw(none any);
  4         7  
  4         1719  
11              
12             with 'YA::CLI::PodRole';
13              
14             requires qw(
15             action
16             run
17             );
18              
19             has _cli_args => (
20             is => 'ro',
21             predicate => '_has_cli_args',
22             writer => '_set_cli_args',
23             init_args => undef,
24             default => sub { {} },
25             );
26              
27             sub cli_options {
28 3     3 1 8 return;
29             }
30              
31             sub BUILD {
32 8     8 0 50 my ($self, $args) = @_;
33              
34 8         25 foreach (keys %$args) {
35 4 100       77 delete $args->{$_} if $self->can($_);
36             }
37 8 100       117 $self->_set_cli_args($args) if %$args;
38             }
39              
40             sub new_from_args {
41 8     8 1 18 my ($class, $args) = @_;
42 8         22 return $class->new($class->get_opts($args));
43             }
44              
45             sub get_opts {
46 8     8 1 16 my ($class, $args) = @_;
47              
48 8         32 my $p = Getopt::Long::Parser->new(
49             config => [qw(no_auto_abbrev) ]
50             );
51              
52 8         784 my %cli_args;
53 8         31 $p->getoptionsfromarray($args, \%cli_args, $class->cli_options);
54 8         17424 return %cli_args;
55             }
56              
57             sub has_action {
58 45     45 1 59 my $self = shift;
59 45         80 my $action = shift;
60 45         51 my $subaction = shift;
61              
62 45 100   45   162 return if none { $action eq $_ } $self->action;
  45         1148  
63              
64 20         68 return $self->has_subaction($subaction);
65             }
66              
67             sub has_subaction {
68 26     26 0 63 my $self = shift;
69 26         38 my $subaction = shift;
70              
71 26 100       163 return -1 unless $self->can('subaction');
72 10 100       34 return 0 unless defined $subaction;
73 7     7   27 return any { $subaction eq $_ } $self->subaction;
  7         82  
74             }
75              
76             1;
77              
78             __END__