blib/lib/App/NDTools/NDProc/Module.pm

filter: Statementstmt 93.0% 94 / 101 Branchbran 91.6% 22 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.4% 22 / 27 Podpod 0.0% 0 / 12 totaltotal 84.1% 138 / 164 CC 15 SCAR 10.7
CC
15
Cov
93%
process
7.0
parse_args
5.1
stash_preserved
4.0
CRAP 2.9
« blib/lib/App/NDTools/NDTool.pm ↑ Summary blib/lib/App/NDTools/NDProc/Module/Pipe.pm »
1 package App::NDTools::NDProc::Module;
2  
3 # base class for ndproc modules
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use App::NDTools::INC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use App::NDTools::NDTool;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use App::NDTools::Util qw(chomp_evaled_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Getopt::Long qw(GetOptionsFromArray :config bundling pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Log::Log4Cli;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Struct::Path 0.80 qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Struct::Path::PerlStyle 0.90 qw(str2path path2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub MODINFO { "n/a" }
Subroutine MODINFO: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 119 ▶sub arg_opts {
Subroutine arg_opts: called CC=1 SCAR=0.0
Pod undocumented
20 119 my $self = shift;
21  
22 return (
23 'blame!' => \$self->{OPTS}->{blame}, # just to set opt in rule
24 'cond=s@' => \$self->{OPTS}->{cond},
25 7 ▶ 'help|h' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 7 $self->{OPTS}->{help} = 1;
27 7 die "!FINISH";
28 },
29 'path=s@' => \$self->{OPTS}->{path},
30 'preserve=s@' => \$self->{OPTS}->{preserve},
31 7 ▶ 'version|V' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 7 $self->{OPTS}->{version} = 1;
33 7 die "!FINISH";
34 },
35 )
36 119 }
37  
38 0 ▶sub check_rule {
Subroutine check_rule: not called CC=1 SCAR=6.9
Pod undocumented
39 0 return $_[0];
40 }
41  
42 88 ▶sub configure {
Subroutine configure: called CC=1 SCAR=0.0
Pod undocumented
43 88 return $_[0];
44 }
45  
46 227 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
47 return {
48 227 disabled => undef,
49 path => [],
50 };
51 }
52  
53 70 ▶sub get_opts {
Subroutine get_opts: called CC=1 SCAR=0.0
Pod undocumented
54 70 return $_[0]->{OPTS};
55 }
56  
57 14 *load_struct = \&App::NDTools::NDTool::load_struct;
58  
59 227 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
60 227 my $self = bless {}, shift;
61  
62 227 $self->{OPTS} = $self->defaults();
63 227 $self->configure;
64  
65 227 return $self;
66 }
67  
68 112 ▶sub parse_args {
Subroutine parse_args: called CC=5 SCAR=16.2
Pod undocumented
69 112 my ($self, $args) = @_;
70  
71 112 ▶ unless (GetOptionsFromArray ($args, $self->arg_opts)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptionsFromArray($args, $self->arg_opts)) 0 112 112
72 0 $self->usage;
73 0 die_fatal "Unsupported opt passed", 1;
74 }
75  
76 112 ▶ if ($self->{OPTS}->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"help"}) 7 105 112
77 7 $self->usage;
78 7 die_info, 0;
79 }
80  
81 105 ▶ if ($self->{OPTS}->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"version"}) 7 98 105
82 7 print $self->VERSION . "\n";
83 7 die_info, 0;
84 }
85  
86 98 ▶ die_fatal $self->{ARG_ERROR}, 1 if (defined $self->{ARG_ERROR});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ARG_ERROR"} 5 93 98
87  
88 93 $self->configure;
89  
90 91 return $self;
91 }
92  
93 107 ▶sub process {
Subroutine process: called CC=7 SCAR=19.5
Pod undocumented
94 107 my ($self, $data, $opts, $source) = @_;
95  
96 107 ▶ $self->check_rule($opts) or die_fatal undef, 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->check_rule($opts) or &die_fatal(undef, 1) 101 2 103
97  
98 $self->stash_preserved($data, $opts->{preserve})
99 101 ▶ if ($opts->{preserve});
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"preserve"} 8 93 101
100  
101 101 for my $path (@{$opts->{path}}) {
102 105 ▶ if (ref $path) { # complex paths passed to mod as is
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 27 78 105
103 27 $self->process_path($data, $path, undef, $opts, $source);
104 } else {
105 0 ▶ log_debug { "Processing path '$path'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
106  
107 78 my $spath = eval { str2path($path) };
108 78 ▶ die_fatal 'Failed to parse path ' . chomp_evaled_error($@), 4
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 77 78
109 if ($@);
110  
111 77 $self->process_path($data, $path, $spath, $opts, $source);
112 }
113 }
114  
115 $self->restore_preserved($data)
116 88 ▶ if ($opts->{preserve});
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"preserve"} 8 80 88
117  
118 88 return $self;
119 }
120  
121 8 ▶sub restore_preserved {
Subroutine restore_preserved: called CC=2 SCAR=6.9
Pod undocumented
122 8 my ($self, $data) = @_;
123  
124 8 ▶ while (@{$self->{_preserved}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_preserved"};}) 18 8 26
125 18 my ($path, $value) = splice @{$self->{_preserved}}, 0, 2;
126 0 ▶ log_debug { "Restoring preserved '" . path2str($path) . "'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
127 18 path(${$data}, $path, assign => $value, expand => 1);
128 }
129  
130 8 return $self;
131 }
132  
133 8 ▶sub stash_preserved {
Subroutine stash_preserved: called CC=4 SCAR=13.9
Pod undocumented
134 8 my ($self, $data, $paths) = @_;
135  
136 8 for my $path (@{$paths}) {
137 0 ▶ log_debug { "Preserving '$path'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
138 8 my $spath = eval { str2path($path) };
139 8 ▶ die_fatal "Failed to parse path ($@)", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
140 8 push @{$self->{_preserved}},
141 36 ▶ map { $_ = ref $_ ? dclone($_) : $_ } # immutable now
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 21 15 36
142 8 path(${$data}, $spath, deref => 1, paths => 1);
143 }
144  
145 8 return $self;
146 }
147  
148 9 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
149 9 require Pod::Find;
150 9 require Pod::Usage;
151  
152 9 Pod::Usage::pod2usage(
153 -exitval => 'NOEXIT',
154 -input => Pod::Find::pod_where({-inc => 1}, ref(shift)),
155 -output => \*STDERR,
156 -sections => 'NAME|DESCRIPTION|OPTIONS',
157 -verbose => 99
158 );
159 }
160  
161 14 1; # End of App::NDTools::NDProc::Module
« blib/lib/App/NDTools/NDTool.pm ↑ Summary blib/lib/App/NDTools/NDProc/Module/Pipe.pm »