File Coverage

blib/lib/App/NDTools/NDProc/Module/Patch.pm
Criterion Covered Total %
statement 42 42 100.0
branch 10 10 100.0
condition n/a
subroutine 12 12 100.0
pod 0 5 0.0
total 64 69 92.7


line stmt bran cond sub pod time code
1             package App::NDTools::NDProc::Module::Patch;
2              
3 3     3   24 use strict;
  3         8  
  3         138  
4 3     3   20 use warnings FATAL => 'all';
  3         8  
  3         122  
5 3     3   19 use parent 'App::NDTools::NDProc::Module';
  3         6  
  3         20  
6              
7 3     3   220 use App::NDTools::Util qw(chomp_evaled_error);
  3         8  
  3         156  
8 3     3   18 use Log::Log4Cli;
  3         8  
  3         276  
9 3     3   21 use Struct::Diff 0.96 qw(patch);
  3         75  
  3         179  
10 3     3   21 use Struct::Path 0.80 qw(path);
  3         45  
  3         1208  
11              
12             our $VERSION = '0.03';
13              
14 1     1 0 7 sub MODINFO { "Apply nested diff to the structure" }
15              
16             sub arg_opts {
17 11     11 0 21 my $self = shift;
18              
19             return (
20             $self->SUPER::arg_opts(),
21             'source=s' => \$self->{OPTS}->{source},
22             'strict!' => \$self->{OPTS}->{strict},
23             )
24 11         34 }
25              
26             sub check_rule {
27 7     7 0 15 my ($self, $rule) = @_;
28              
29             die_fatal "Source file should be specified", 1
30 7 100       22 unless ($rule->{source});
31              
32 6 100       10 push @{$rule->{path}}, '' unless (@{$rule->{path}});
  2         7  
  6         23  
33              
34 6         18 return $self;
35             }
36              
37             sub configure {
38 26     26 0 76 my $self = shift;
39              
40             # to prevent source resolve to target (ndproc defaults)
41             delete $self->{OPTS}->{source}
42 26 100       101 unless (defined $self->{OPTS}->{source});
43             }
44              
45             sub process_path {
46 6     6 0 16 my ($self, $data, $path, $spath, $opts, $source) = @_;
47              
48 6         11 my @refs = eval { path(${$data}, $spath, strict => $opts->{strict}) };
  6         10  
  6         24  
49 6 100       658 die_fatal "Failed to lookup path '$path'", 4 if ($@);
50              
51 5         12 for (@refs) {
52 5         11 eval { patch(${$_}, $source) };
  5         6  
  5         18  
53 5 100       582 die_fatal chomp_evaled_error($@), 8 if ($@);
54             }
55             }
56              
57             1; # End of App::NDTools::NDProc::Module::Patch
58              
59             __END__