File Coverage

blib/lib/App/NDTools/NDProc/Module.pm
Criterion Covered Total %
statement 66 84 78.5
branch 6 12 50.0
condition n/a
subroutine 16 26 61.5
pod 0 12 0.0
total 88 134 65.6


line stmt bran cond sub pod time code
1             package App::NDTools::NDProc::Module;
2              
3             # base class for ndproc modules
4              
5 5     5   2338 use strict;
  5         11  
  5         156  
6 5     5   31 use warnings FATAL => 'all';
  5         8  
  5         156  
7              
8 5     5   1697 use App::NDTools::INC;
  5         12  
  5         28  
9 5     5   1739 use App::NDTools::NDTool;
  5         14  
  5         208  
10 5     5   36 use Getopt::Long qw(GetOptionsFromArray :config bundling pass_through);
  5         13  
  5         994  
11 5     5   813 use Log::Log4Cli;
  5         29  
  5         365  
12 5     5   4568 use Storable qw(dclone);
  5         12995  
  5         339  
13 5     5   36 use Struct::Path 0.80 qw(path);
  5         88  
  5         263  
14 5     5   2191 use Struct::Path::PerlStyle 0.80 qw(str2path path2str);
  5         689469  
  5         4543  
15              
16 0     0 0 0 sub MODINFO { "n/a" }
17              
18             sub arg_opts {
19 0     0 0 0 my $self = shift;
20              
21             return (
22             'blame!' => \$self->{OPTS}->{blame}, # just to set opt in rule
23 0     0   0 'help|h' => sub { $self->usage(); exit 0 },
  0         0  
24             'path=s@' => \$self->{OPTS}->{path},
25             'preserve=s@' => \$self->{OPTS}->{preserve},
26 0     0   0 'version|V' => sub { print $self->VERSION . "\n"; exit 0 },
  0         0  
27             )
28 0         0 }
29              
30             sub check_rule {
31 0     0 0 0 return $_[0];
32             }
33              
34             sub configure {
35 4     4 0 7 return $_[0];
36             }
37              
38             sub defaults {
39             return {
40 8     8 0 66 disabled => undef,
41             path => [],
42             };
43             }
44              
45             sub get_opts {
46 0     0 0 0 return $_[0]->{OPTS};
47             }
48              
49             *load_struct = \&App::NDTools::NDTool::load_struct;
50              
51             sub new {
52 8     8 0 5933 my $self = bless {}, shift;
53              
54 8         42 $self->{OPTS} = $self->defaults();
55 8         51 $self->configure;
56              
57 8         31 return $self;
58             }
59              
60             sub parse_args {
61 0     0 0 0 my ($self, $args) = @_;
62              
63 0 0       0 unless (GetOptionsFromArray ($args, $self->arg_opts)) {
64 0         0 $self->usage;
65 0         0 die_fatal "Unsupported opt passed", 1;
66             }
67 0         0 $self->configure;
68              
69 0         0 return $self;
70             }
71              
72             sub process {
73 2     2 0 1715 my ($self, $data, $opts, $source) = @_;
74              
75 2 50       7 $self->check_rule($opts) or die_fatal undef, 1;
76              
77             $self->stash_preserved($data, $opts->{preserve})
78 2 50       13 if ($opts->{preserve});
79              
80 2         6 for my $path (@{$opts->{path}}) {
  2         4  
81 2     0   14 log_debug { "Processing path '$path'" };
  0         0  
82 2         16 $self->process_path($data, $path, $opts, $source);
83             }
84              
85             $self->restore_preserved($data)
86 2 50       12 if ($opts->{preserve});
87              
88 2         5 return $self;
89             }
90              
91             sub restore_preserved {
92 2     2 0 38 my ($self, $data) = @_;
93              
94 2         3 while (@{$self->{_preserved}}) {
  5         187  
95 3         5 my ($path, $value) = splice @{$self->{_preserved}}, 0, 2;
  3         39  
96 3     0   25 log_debug { "Restoring preserved '" . path2str($path) . "'" };
  0         0  
97 3         17 path(${$data}, $path, assign => $value, expand => 1);
  3         9  
98             }
99              
100 2         5 return $self;
101             }
102              
103             sub stash_preserved {
104 2     2 0 5 my ($self, $data, $paths) = @_;
105              
106 2         3 for my $path (@{$paths}) {
  2         4  
107 2     0   14 log_debug { "Preserving '$path'" };
  0         0  
108 2         12 my $spath = eval { str2path($path) };
  2         6  
109 2 50       3560 die_fatal "Failed to parse path ($@)", 4 if ($@);
110 2         5 push @{$self->{_preserved}},
111 6 100       298 map { $_ = ref $_ ? dclone($_) : $_ } # immutable now
112 2         4 path(${$data}, $spath, deref => 1, paths => 1);
  2         7  
113             }
114              
115 2         5 return $self;
116             }
117              
118             sub usage {
119 4     4 0 24 require Pod::Find;
120 4         648 require Pod::Usage;
121              
122 4         41025 Pod::Usage::pod2usage(
123             -exitval => 'NOEXIT',
124             -input => Pod::Find::pod_where({-inc => 1}, ref(shift)),
125             -output => \*STDERR,
126             -sections => 'NAME|DESCRIPTION|OPTIONS',
127             -verbose => 99
128             );
129             }
130              
131             1; # End of App::NDTools::NDProc::Module