File Coverage

blib/lib/App/NDTools/NDProc/Module.pm
Criterion Covered Total %
statement 74 84 88.1
branch 10 12 83.3
condition n/a
subroutine 20 26 76.9
pod 0 12 0.0
total 104 134 77.6


line stmt bran cond sub pod time code
1             package App::NDTools::NDProc::Module;
2              
3             # base class for ndproc modules
4              
5 10     10   4879 use strict;
  10         42  
  10         344  
6 10     10   54 use warnings FATAL => 'all';
  10         32  
  10         416  
7              
8 10     10   1894 use App::NDTools::INC;
  10         26  
  10         82  
9 10     10   1877 use App::NDTools::NDTool;
  10         31  
  10         398  
10 10     10   76 use Getopt::Long qw(GetOptionsFromArray :config bundling pass_through);
  10         25  
  10         84  
11 10     10   2124 use Log::Log4Cli;
  10         25  
  10         986  
12 10     10   5965 use Storable qw(dclone);
  10         13013  
  10         675  
13 10     10   78 use Struct::Path 0.80 qw(path);
  10         238  
  10         583  
14 10     10   2385 use Struct::Path::PerlStyle 0.80 qw(str2path path2str);
  10         678747  
  10         8912  
15              
16 0     0 0 0 sub MODINFO { "n/a" }
17              
18             sub arg_opts {
19 59     59 0 114 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 59         1091 }
29              
30             sub check_rule {
31 26     26 0 83 return $_[0];
32             }
33              
34             sub configure {
35 74     74 0 137 return $_[0];
36             }
37              
38             sub defaults {
39             return {
40 145     145 0 893 disabled => undef,
41             path => [],
42             };
43             }
44              
45             sub get_opts {
46 38     38 0 189 return $_[0]->{OPTS};
47             }
48              
49             *load_struct = \&App::NDTools::NDTool::load_struct;
50              
51             sub new {
52 145     145 0 6410 my $self = bless {}, shift;
53              
54 145         504 $self->{OPTS} = $self->defaults();
55 145         630 $self->configure;
56              
57 145         489 return $self;
58             }
59              
60             sub parse_args {
61 59     59 0 170 my ($self, $args) = @_;
62              
63 59 50       220 unless (GetOptionsFromArray ($args, $self->arg_opts)) {
64 0         0 $self->usage;
65 0         0 die_fatal "Unsupported opt passed", 1;
66             }
67 59         21093 $self->configure;
68              
69 58         246 return $self;
70             }
71              
72             sub process {
73 80     80 0 1471 my ($self, $data, $opts, $source) = @_;
74              
75 80 100       254 $self->check_rule($opts) or die_fatal undef, 1;
76              
77             $self->stash_preserved($data, $opts->{preserve})
78 78 100       309 if ($opts->{preserve});
79              
80 78         143 for my $path (@{$opts->{path}}) {
  78         217  
81 80     0   543 log_debug { "Processing path '$path'" };
  0         0  
82 80         559 $self->process_path($data, $path, $opts, $source);
83             }
84              
85             $self->restore_preserved($data)
86 72 100       3539 if ($opts->{preserve});
87              
88 72         261 return $self;
89             }
90              
91             sub restore_preserved {
92 5     5 0 31 my ($self, $data) = @_;
93              
94 5         26 while (@{$self->{_preserved}}) {
  20         2192  
95 15         31 my ($path, $value) = splice @{$self->{_preserved}}, 0, 2;
  15         57  
96 15     0   119 log_debug { "Restoring preserved '" . path2str($path) . "'" };
  0         0  
97 15         65 path(${$data}, $path, assign => $value, expand => 1);
  15         63  
98             }
99              
100 5         24 return $self;
101             }
102              
103             sub stash_preserved {
104 5     5 0 18 my ($self, $data, $paths) = @_;
105              
106 5         12 for my $path (@{$paths}) {
  5         18  
107 5     0   55 log_debug { "Preserving '$path'" };
  0         0  
108 5         34 my $spath = eval { str2path($path) };
  5         28  
109 5 50       12233 die_fatal "Failed to parse path ($@)", 4 if ($@);
110 5         18 push @{$self->{_preserved}},
111 30 100       1624 map { $_ = ref $_ ? dclone($_) : $_ } # immutable now
112 5         12 path(${$data}, $spath, deref => 1, paths => 1);
  5         27  
113             }
114              
115 5         17 return $self;
116             }
117              
118             sub usage {
119 4     4 0 29 require Pod::Find;
120 4         652 require Pod::Usage;
121              
122 4         41360 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