Extension/Component/InstallCallback.pm

filter: Statementstmt 33.3% 10 / 30 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 0.0% 0 / 4 totaltotal 23.0% 12 / 52 CC 7 SCAR 21.0
CC
7
Cov
24%
new_callback
12.0
add_callback
12.0
exec_callback
6.0
CRAP 8.2
« Extension/Component/OriginalArgv.pm ↑ Summary Extension/Component/RunCommand.pm »
1 package App::CLI::Extension::Component::InstallCallback;
2  
3 =pod
4  
5 =head1 NAME
6  
7 App::CLI::Extension::Component::InstallCallback - for App::CLI::Extension install callback module
8  
9 =head1 VERSION
10  
11 1.421
12  
13 =head1 SYNOPSIS
14
15 # MyApp.pm
16 package MyApp;
17
18 use strict;
19 use base qw(App::CLI::Extension);
20
21 # MyApp/Hello.pm
22 package MyApp::Hello;
23 use strict;
24 use feature ":5.10.0";
25 use base qw(App::CLI::Command);
26 use constants options => ("runmode=s" => "runmode");
27
28 sub prerun {
29
30 my($self, @argv) = @_;
31 $self->new_callback("view", sub {
32 my($self, @args) = @_;
33 # anything view to do...
34 foreach $list (@{$self->anything_all_list}) {
35 printf "%d: %s\n", $list->id, $list->name;
36 }
37 });
38 $self->new_callback("exec", sub {
39 my($self, @args) = @_;
40 # anything execute to do...
41 $self->anything_execute(@args);
42 });
43 $self->>maybe::next::method(@argv);
44 }
45
46 sub run {
47
48 my($self, @args) = @_;
49 my $runmode = $self->{runmode};
50 if ($self->exists_callback($runmode)) {
51 $self->exec_callback($runmode, @args);
52 } else {
53 die "invalid runmode!!";
54 }
55 }
56
57 # myapp
58 #!/usr/bin/perl
59
60 use strict;
61 use MyApp;
62
63 MyApp->dispatch;
64
65 # execute view callback
66 [kurt@localhost ~] myapp hello --runmode=view
67 1: melon
68 2: banana
69 .
70 .
71 .
72  
73 =cut
74  
75 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 18 ▶use base qw(Class::Accessor::Grouped);
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 18 __PACKAGE__->mk_group_accessors( "inherited" => "_install_callback" );
79 18 __PACKAGE__->_install_callback({});
80 18 our $VERSION = '1.421';
81  
82 0 ▶sub new_callback {
Subroutine new_callback: not called CC=3 SCAR=24.8
Pod undocumented
83  
84 0 my($self, $install, $callback) = @_;
85 0 ▶ if ($self->exists_callback($install)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists_callback($install)) 0 0 0
86 0 die "already exists $install";
87 }
88 0 $self->_install_callback->{$install} = [];
89 0 ▶ $self->add_callback($install, $callback) if defined $callback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback 0 0 0
90 }
91  
92 0 ▶sub add_callback {
Subroutine add_callback: not called CC=3 SCAR=24.8
Pod undocumented
93  
94 0 my($self, $install, $callback) = @_;
95 0 ▶ if (!$self->exists_callback($install)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists_callback($install)) 0 0 0
96 0 die "non install callback: $install";
97 }
98 0 ▶ if(ref($callback) ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback ne "CODE") 0 0 0
99 0 die "\$callback is not CODE";
100 }
101 0 push @{$self->_install_callback->{$install}}, $callback;
102 }
103  
104 0 ▶sub exec_callback {
Subroutine exec_callback: not called CC=2 SCAR=17.9
Pod undocumented
105  
106 0 my($self, $install, @args) = @_;
107 0 ▶ if (!$self->exists_callback($install)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->exists_callback($install)) 0 0 0
108 0 die "non install callback: $install";
109 }
110 0 map { $self->$_(@args) } @{$self->_install_callback->{$install}};
111 }
112  
113 0 ▶sub exists_callback {
Subroutine exists_callback: not called CC=2 SCAR=17.9
Pod undocumented
114  
115 0 my($self, $install) = @_;
116 0 ▶ return exists $self->_install_callback->{$install} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->_install_callback->{$install} ? : 0 0 0
117 }
118  
119 18 1;
120  
121 __END__
« Extension/Component/OriginalArgv.pm ↑ Summary Extension/Component/RunCommand.pm »