blib/lib/JIP/DataPath.pm

filter: Statementstmt 95.7% 91 / 95 Branchbran 85.0% 34 / 40 Conditioncond 51.2% 21 / 41 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 100% 17 / 17 Podpod 100% 10 / 10 totaltotal 78.4% 178 / 227 CC 38 SCAR 21.0
CC
38
Cov
83%
get_new
14.2
set
10.2
_accessor
10.1
CRAP 8.2
↑ Summary
1 package JIP::DataPath;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.044';
11  
12 1 our @EXPORT_OK = qw(path);
13  
14 4 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
15 4 my ($document) = @ARG;
16  
17 4 return __PACKAGE__->new( document => $document );
18 }
19  
20 13 ▶sub default_value {
Subroutine default_value: called CC=1 SCAR=0.0
Pod documented
21 13 return __PACKAGE__ . '::default_value';
22 }
23  
24 9 ▶sub is_default_value {
Subroutine is_default_value: called CC=4 SCAR=13.9
Pod documented
25 9 my ( $self, $value ) = @ARG;
26  
27 9 ▶ return 0 if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
28 8 ▶ return 0 if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 4 4 8
29 4 ▶ return 0 if $value ne $self->default_value();
Branch: 1 branch
BranchTrueFalseTotal
if $value ne $self->default_value 3 1 4
30  
31 1 return 1;
32 }
33  
34 67 ▶sub document {
Subroutine document: called CC=1 SCAR=0.0
Pod documented
35 67 my ($self) = @ARG;
36  
37 67 return $self->{document};
38 }
39  
40 21 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
41 21 my ( $class, %param ) = @ARG;
42  
43 # Mandatory params
44 21 ▶ if ( !exists $param{document} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param{"document"}) 1 20 21
45 1 croak 'Mandatory argument "document" is missing';
46 }
47  
48 return bless(
49 {
50 document => $param{document},
51 },
52 20 $class,
53 );
54 }
55  
56 27 ▶sub get {
Subroutine get: called CC=9 SCAR=23.1
Pod documented
57 27 my ( $self, $path_parts, $default_value ) = @ARG;
58  
59 27 ▶ if ( @{$path_parts} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$path_parts;} == 0) 7 20 27
60 7 return $self->document();
61 }
62  
63 20 my ( $contains, $context ) = $self->_accessor($path_parts);
64  
65 20 ▶ return $default_value if !$contains;
Branch: 1 branch
BranchTrueFalseTotal
unless $contains 10 10 20
66  
67 10 ▶ my $last_part = $path_parts->[-1] // q{};
Condition50%1/2
$path_parts->[-1] // ""
l!l
100
MC/DC0%0/1
$path_parts->[-1] // ""
$path_parts->[-1]
Truth table50%1/2
A
A= $path_parts->[-1]
Aresult
11
00
68 10 ▶ my $type = ref $context // q{};
Condition50%1/2
ref $context // ""
l!l
100
MC/DC0%0/1
ref $context // ""
ref $context
Truth table50%1/2
A
A= ref $context
Aresult
11
00
69  
70 10 ▶ if ( $type eq 'HASH' && length $last_part ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH' and length $last_part) { } 8 2 10
elsif ($type eq 'ARRAY' and $last_part =~ /^\d+$/x) { } 2 0 2
Condition66%2/3
$type eq "HASH" and length $last_part
!ll&&!rl&&r
208
MC/DC50%1/2
$type eq "HASH" and length $last_part
$type eq "HASH" length $last_part
Truth table66%2/3
A and B
A= $type eq "HASH"B= length $last_part
ABresult
0X0
100
111
71 8 return $context->{$last_part};
72 }
73 ▶ elsif ( $type eq 'ARRAY' && $last_part =~ m{^\d+$}x ) {
Condition33%1/3
$type eq "ARRAY" and $last_part =~ /^\d+$/x
!ll&&!rl&&r
002
MC/DC0%0/2
$type eq "ARRAY" and $last_part =~ /^\d+$/x
$type eq "ARRAY" $last_part =~ /^\d+$/x
Truth table33%1/3
A and B
A= $type eq "ARRAY"B= $last_part =~ /^\d+$/x
ABresult
0X0
100
111
74 2 return $context->[$last_part];
75 }
76  
77 0 return $default_value;
78 } ## end sub get
79  
80 5 ▶sub get_new {
Subroutine get_new: called CC=9 SCAR=26.5
Pod documented
81 5 my ( $self, $path_parts, $default_value ) = @ARG;
82  
83 5 ▶ if ( @{$path_parts} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$path_parts;} == 0) 2 3 5
84 2 return path( $self->document() );
85 }
86  
87 3 my ( $contains, $context ) = $self->_accessor($path_parts);
88  
89 3 ▶ return $default_value if !$contains;
Branch: 1 branch
BranchTrueFalseTotal
unless $contains 2 1 3
90  
91 1 ▶ my $last_part = $path_parts->[-1] // q{};
Condition50%1/2
$path_parts->[-1] // ""
l!l
10
MC/DC0%0/1
$path_parts->[-1] // ""
$path_parts->[-1]
Truth table50%1/2
A
A= $path_parts->[-1]
Aresult
11
00
92 1 ▶ my $type = ref $context // q{};
Condition50%1/2
ref $context // ""
l!l
10
MC/DC0%0/1
ref $context // ""
ref $context
Truth table50%1/2
A
A= ref $context
Aresult
11
00
93  
94 1 ▶ if ( $type eq 'HASH' && length $last_part ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH' and length $last_part) { } 1 0 1
elsif ($type eq 'ARRAY' and $last_part =~ /^\d+$/x) { } 0 0 0
Condition33%1/3
$type eq "HASH" and length $last_part
!ll&&!rl&&r
001
MC/DC0%0/2
$type eq "HASH" and length $last_part
$type eq "HASH" length $last_part
Truth table33%1/3
A and B
A= $type eq "HASH"B= length $last_part
ABresult
0X0
100
111
95 1 return path( $context->{$last_part} );
96 }
97 ▶ elsif ( $type eq 'ARRAY' && $last_part =~ m{^\d+$}x ) {
Condition0%0/3
$type eq "ARRAY" and $last_part =~ /^\d+$/x
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "ARRAY" and $last_part =~ /^\d+$/x
$type eq "ARRAY" $last_part =~ /^\d+$/x
Truth table0%0/3
A and B
A= $type eq "ARRAY"B= $last_part =~ /^\d+$/x
ABresult
0X0
100
111
98 0 return path( $context->[$last_part] );
99 }
100  
101 0 return $default_value;
102 } ## end sub get_new
103  
104 9 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
105 9 my ( $self, @xargs ) = @ARG;
106  
107 9 my ($contains) = $self->_accessor(@xargs);
108  
109 9 return $contains;
110 }
111  
112 9 ▶sub set {
Subroutine set: called CC=9 SCAR=23.2
Pod documented
113 9 my ( $self, $path_parts, $value ) = @ARG;
114  
115 9 ▶ if ( @{$path_parts} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$path_parts;} == 0) 4 5 9
116 4 $self->_set_document($value);
117 4 return 1;
118 }
119  
120 5 my ( $contains, $context ) = $self->_accessor($path_parts);
121  
122 5 ▶ return 0 if !$contains;
Branch: 1 branch
BranchTrueFalseTotal
unless $contains 0 5 5
123  
124 5 ▶ my $last_part = $path_parts->[-1] // q{};
Condition50%1/2
$path_parts->[-1] // ""
l!l
50
MC/DC0%0/1
$path_parts->[-1] // ""
$path_parts->[-1]
Truth table50%1/2
A
A= $path_parts->[-1]
Aresult
11
00
125 5 ▶ my $type = ref $context // q{};
Condition50%1/2
ref $context // ""
l!l
50
MC/DC0%0/1
ref $context // ""
ref $context
Truth table50%1/2
A
A= ref $context
Aresult
11
00
126  
127 5 ▶ if ( $type eq 'HASH' && length $last_part ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH' and length $last_part) { } 4 1 5
elsif ($type eq 'ARRAY' and $last_part =~ /^\d+$/x) { } 1 0 1
Condition66%2/3
$type eq "HASH" and length $last_part
!ll&&!rl&&r
104
MC/DC50%1/2
$type eq "HASH" and length $last_part
$type eq "HASH" length $last_part
Truth table66%2/3
A and B
A= $type eq "HASH"B= length $last_part
ABresult
0X0
100
111
128 4 $context->{$last_part} = $value;
129 4 return 1;
130 }
131 ▶ elsif ( $type eq 'ARRAY' && $last_part =~ m{^\d+$}x ) {
Condition33%1/3
$type eq "ARRAY" and $last_part =~ /^\d+$/x
!ll&&!rl&&r
001
MC/DC0%0/2
$type eq "ARRAY" and $last_part =~ /^\d+$/x
$type eq "ARRAY" $last_part =~ /^\d+$/x
Truth table33%1/3
A and B
A= $type eq "ARRAY"B= $last_part =~ /^\d+$/x
ABresult
0X0
100
111
132 1 $context->[$last_part] = $value;
133 1 return 1;
134 }
135  
136 0 return 0;
137 } ## end sub set
138  
139 5 ▶sub perform {
Subroutine perform: called CC=1 SCAR=0.0
Pod documented
140 5 my ( $self, $method, $path_parts, @xargs ) = @ARG;
141  
142 5 return $self->$method( $path_parts, @xargs );
143 }
144  
145 4 ▶sub _set_document {
Subroutine _set_document: called CC=1 SCAR=0.0
146 4 my ( $self, $document ) = @ARG;
147  
148 4 $self->{document} = $document;
149  
150 4 return;
151 }
152  
153 46 ▶sub _accessor {
Subroutine _accessor: called CC=10 SCAR=23.1
154 46 my ( $self, $path_parts ) = @ARG;
155  
156 46 my $context = $self->document();
157 46 my $last_index = $#{$path_parts};
158  
159 46 foreach my $part_index ( 0 .. $last_index ) {
160 80 my $part = $path_parts->[$part_index];
161 80 ▶ my $type = ref $context // q{};
Condition50%1/2
ref $context // ""
l!l
800
MC/DC0%0/1
ref $context // ""
ref $context
Truth table50%1/2
A
A= ref $context
Aresult
11
00
162 80 my $last = $part_index == $last_index;
163  
164 80 ▶ if ( $type eq 'HASH' && exists $context->{$part} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH' and exists $context->{$part}) { } 49 31 80
elsif ($type eq 'ARRAY' and $part =~ /^\d+$/x and @{$context;} > $part) { } 15 16 31
Condition100%3/3
$type eq "HASH" and exists $context->{$part}
!ll&&!rl&&r
25649
MC/DC100%2/2
$type eq "HASH" and exists $context->{$part}
$type eq "HASH" exists $context->{$part}
Truth table100%3/3
A and B
A= $type eq "HASH"B= exists $context->{$part}
ABresult
0X0
100
111
165 49 ▶ return ( 1, $context ) if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 20 29 49
166  
167 29 $context = $context->{$part};
168 }
169 15 ▶ elsif ( $type eq 'ARRAY' && $part =~ m{^\d+$}x && @{$context} > $part ) {
Condition66%4/6
$type eq "ARRAY" and $part =~ /^\d+$/x and @{$context;} > $part
!ll&&!rl&&r
16015
$type eq "ARRAY" and $part =~ /^\d+$/x
!ll&&!rl&&r
16015
MC/DC33%1/3
$type eq "ARRAY" and $part =~ /^\d+$/x and @{$context;} > $part
$type eq "ARRAY" $part =~ /^\d+$/x @{$context;} > $part
Truth table50%2/4
A and B and C
A= $type eq "ARRAY"B= $part =~ /^\d+$/xC= @{$context;} > $part
ABCresult
0XX0
10X0
1100
1111
170 15 ▶ return ( 1, $context ) if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 7 8 15
171  
172 8 $context = $context->[$part];
173 }
174 else {
175 16 return ( 0, undef );
176 }
177 }
178  
179 3 return ( 1, $context );
180 } ## end sub _accessor
181  
182 1 1;
183  
184 __END__
↑ Summary