Footprintless/App/ActionCommand.pm

filter: Statementstmt 63.7% 44 / 69 Branchbran 31.8% 7 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.2% 9 / 14 Podpod 100% 5 / 5 totaltotal 54.1% 65 / 120 CC 15 SCAR 20.1
CC
15
Cov
53%
description
20.0
_format_actions
12.0
usage
12.0
CRAP 7.4
« Footprintless/Mixins.pm ↑ Summary Footprintless/App/Command/service.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::App::ActionCommand;
5 5 $Footprintless::App::ActionCommand::VERSION = '1.29';
6 # ABSTRACT: A base class for action commands
7 # PODNAME: Footprintless::App::ActionCommand
8  
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Footprintless::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Getopt::Long::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 my $logger = Log::Any->get_logger();
15  
16 0 ▶sub _coord_desc {
Subroutine _coord_desc: not called CC=1 SCAR=6.9
17 0 return 'COORD';
18 }
19  
20 0 ▶sub _default_action {
Subroutine _default_action: not called CC=1 SCAR=6.9
21 0 return;
22 }
23  
24 0 ▶sub description {
Subroutine description: not called CC=4 SCAR=30.0
Pod documented
25 0 my ($self) = @_;
26  
27 0 ▶ if ( $self->{action} && !$self->{is_default_action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'action'} and not $self->{'is_default_action'}) { } 0 0 0
Condition0%0/3
$self->{"action"} and not $self->{"is_default_action"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"action"} and not $self->{"is_default_action"}
$self->{"action"} not $self->{"is_default_action"}
Truth table0%0/3
A and not(B)
A= $self->{"action"}B= not $self->{"is_default_action"}
ABresult
0X0
100
111
28 0 return $self->{action}->description();
29 }
30 else {
31 0 require Footprintless::App::DocumentationUtil;
32 0 my $pod_description = Footprintless::App::DocumentationUtil::description($self);
33 0 my %actions = $self->_actions();
34  
35 0 my $default_action = $self->_default_action();
36 0 ▶ $default_action =
Branch: 1 branch
BranchTrueFalseTotal
$default_action ? : 0 0 0
37 $default_action
38 ? "\n\nDefault action: $default_action"
39 : '';
40  
41 return
42 0 $pod_description
43 . "\nAvailable actions:\n\n"
44 . $self->_format_actions( $self->_actions() )
45 . $default_action;
46 }
47 }
48  
49 8 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
50 8 my ( $self, $opts, $args ) = @_;
51  
52 8 $self->{action}->execute( $self->{action_opts}, $self->{action_args} );
53 }
54  
55 0 ▶sub _format_actions {
Subroutine _format_actions: not called CC=3 SCAR=24.8
56 0 my ( $self, %actions ) = @_;
57  
58 0 my %abstracts = ();
59 0 my $max_length = 0;
60 0 foreach my $key ( keys(%actions) ) {
61 0 my $length = length($key);
62 0 $abstracts{$key} = Footprintless::App::DocumentationUtil::abstract( $actions{$key} );
63 0 ▶ $max_length = $length if ( $length > $max_length );
Branch: 1 branch
BranchTrueFalseTotal
if $length > $max_length 0 0 0
64 }
65  
66 return join( "\n",
67 0 map { sprintf( " %${max_length}s: %s", $_, $abstracts{$_} ); } sort keys(%actions) );
68 }
69  
70 8 ▶sub prepare {
Subroutine prepare: called CC=4 SCAR=14.3
Pod documented
71 8 my ( $class, $app, @args ) = @_;
72  
73 8 my ( $opts, $remaining_args, %fields ) =
74 $class->_process_args( \@args, $class->usage_desc(), $class->opt_spec() );
75  
76 8 my ( $coordinate, $action_name, @action_args ) = @$remaining_args;
77 8 $fields{coordinate} = $coordinate;
78  
79 8 ▶ if ($action_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action_name) { } 8 0 8
80 8 $fields{action_name} = $action_name;
81 }
82 else {
83 0 $fields{action_name} = $class->_default_action();
84 0 $fields{is_default_action} = 1;
85 }
86  
87 8 ▶ if ( $fields{action_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields{"action_name"}) 8 0 8
88 8 my %actions = $class->_actions();
89 8 my $action = $actions{ $fields{action_name} };
90 8 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 8 0 8
91 8 { eval "require $action" }; ## no critic
92 8 ( $fields{action}, $fields{action_opts}, $fields{action_args} ) =
93 $action->prepare( $app, $app->footprintless(), $coordinate, @action_args );
94 }
95 }
96  
97 return (
98 8 $class->new(
99 { app => $app,
100 %fields
101 }
102 ),
103 $opts,
104 $remaining_args
105 );
106 }
107  
108 0 ▶sub usage {
Subroutine usage: not called CC=3 SCAR=24.8
Pod documented
109 0 my ($self) = @_;
110 return ( $self->{action} && !$self->{is_default_action} )
111 ? $self->{action}->usage()
112 0 ▶ : $self->{usage};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'action'} && !$self->{'is_default_action'} ? : 0 0 0
Condition0%0/3
$self->{"action"} and not $self->{"is_default_action"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"action"} and not $self->{"is_default_action"}
$self->{"action"} not $self->{"is_default_action"}
Truth table0%0/3
A and not(B)
A= $self->{"action"}B= not $self->{"is_default_action"}
ABresult
0X0
100
111
113 }
114  
115 8 ▶sub validate_args {
Subroutine validate_args: called CC=5 SCAR=17.5
Pod documented
116 8 my ( $self, $opts, $args ) = @_;
117  
118 8 ▶ $self->usage_error("coordinate required") unless ( $self->{coordinate} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"coordinate"} 0 8 8
119 8 ▶ $self->usage_error("action required") unless ( $self->{action_name} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"action_name"} 0 8 8
120 $self->usage_error("invalid action [$self->{action_name}]")
121 8 ▶ unless ( $self->{action} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"action"} 0 8 8
122  
123 8 eval { $self->{action}->validate_args( $self->{action_opts}, $self->{action_args} ); };
124 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
125 0 $self->usage_error($@);
126 }
127 }
128  
129 5 1;
130  
131 __END__
« Footprintless/Mixins.pm ↑ Summary Footprintless/App/Command/service.pm »