File Coverage

blib/lib/App/NDTools/NDProc/Module/Insert.pm
Criterion Covered Total %
statement 57 58 98.2
branch 16 16 100.0
condition 6 6 100.0
subroutine 18 19 94.7
pod 0 5 0.0
total 97 104 93.2


line stmt bran cond sub pod time code
1             package App::NDTools::NDProc::Module::Insert;
2              
3 4     4   65594 use strict;
  4         18  
  4         157  
4 4     4   27 use warnings FATAL => 'all';
  4         8  
  4         219  
5 4     4   452 use parent 'App::NDTools::NDProc::Module';
  4         312  
  4         29  
6              
7 4     4   247 use Log::Log4Cli;
  4         11  
  4         350  
8 4     4   25 use Scalar::Util qw(looks_like_number);
  4         8  
  4         207  
9 4     4   23 use Struct::Path 0.80 qw(path);
  4         73  
  4         199  
10              
11 4     4   30 use App::NDTools::Util qw(chomp_evaled_error);
  4         8  
  4         194  
12 4     4   27 use App::NDTools::Slurp qw(s_decode);
  4         10  
  4         3171  
13              
14             our $VERSION = '0.20';
15              
16 1     1 0 6 sub MODINFO { "Insert value into structure" }
17              
18             sub arg_opts {
19 35     35 0 58 my $self = shift;
20              
21             return (
22             $self->SUPER::arg_opts(),
23             'boolean=s' => sub {
24 10 100 100 10   11809 if ($_[1] eq '1' or $_[1] =~ /^(T|t)rue$/) {
    100 100        
25 3         20 $self->{OPTS}->{value} = JSON::true;
26             } elsif ($_[1] eq '0' or $_[1] =~ /^(F|f)alse$/) {
27 3         53 $self->{OPTS}->{value} = JSON::false;
28             } else {
29 4         14 $self->{ARG_ERROR} = "Unsuitable value for --boolean";
30 4         18 die "!FINISH";
31             }
32             },
33             'file|f=s' => \$self->{OPTS}->{file},
34             'file-fmt=s' => \$self->{OPTS}->{'file-fmt'},
35 2     2   2294 'null|undef' => sub { $self->{OPTS}->{value} = undef },
36             'number=s' => sub {
37 6 100   6   6835 if (looks_like_number($_[1])) {
38 5         25 $self->{OPTS}->{value} = 0 + $_[1];
39             } else {
40 1         4 $self->{ARG_ERROR} = "Unsuitable value for --number";
41 1         6 die "!FINISH";
42             }
43             },
44 8     8   9489 'string|value=s' => sub { $self->{OPTS}->{value} = $_[1] },
45             'structure=s' => \$self->{OPTS}->{structure},
46             )
47 35         123 }
48              
49             sub check_rule {
50 26     26 0 61 my ($self, $rule) = @_;
51 26         59 my $out = $self;
52              
53 26 100       85 unless (exists $rule->{value}) {
54 1     1   9 log_error { "Value to insert should be defined" };
  1         80  
55 1         7 $out = undef;
56             }
57              
58 26 100       52 push @{$rule->{path}}, '' unless (@{$rule->{path}});
  1         4  
  26         86  
59              
60 26         90 return $out;
61             }
62              
63             sub configure {
64 87     87 0 149 my $self = shift;
65              
66             $self->{OPTS}->{value} =
67             s_decode(delete $self->{OPTS}->{structure}, 'JSON')
68 87 100       275 if (defined $self->{OPTS}->{structure});
69              
70             $self->{OPTS}->{value} =
71             $self->load_struct($self->{OPTS}->{file}, $self->{OPTS}->{'file-fmt'})
72 86 100       284 if (defined $self->{OPTS}->{file});
73             }
74              
75             sub process_path {
76 26     26 0 115 my ($self, $data, $path, $spath, $opts) = @_;
77              
78 26     0   161 log_info { 'Updating path "' . $path . '"' };
  0         0  
79 26         137 eval { path(${$data}, $spath, assign => $opts->{value}, expand => 1) };
  26         58  
  26         128  
80 26 100       5265 die_fatal "Failed to lookup path '$path' (" .
81             chomp_evaled_error($@) . ")", 4 if ($@);
82             }
83              
84              
85             1; # End of App::NDTools::NDProc::Module::Insert
86              
87             __END__