lib/JSON/Pointer/Extend.pm

filter: Statementstmt 93.5% 58 / 62 Branchbran 83.3% 20 / 24 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 87.0% 94 / 108 CC 19 SCAR 16.8
CC
19
Cov
89%
_recursive_process
10.1
document
4.2
pointer
3.1
CRAP 5.4
↑ Summary
1 package JSON::Pointer::Extend;
2  
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.02';
11  
12 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
13 3 my ($class, %opt) = @_;
14 3 my $self = {};
15 3 bless $self, $class;
16  
17 3 ▶ $opt{'-pointer'} && $self->pointer(delete $opt{'-pointer'});
Branch: 1 branch
BranchTrueFalseTotal
$opt{"-pointer"} and $self->pointer(delete $opt{"-pointer"}) 2 1 3
18 3 ▶ $opt{'-document'} && $self->document(delete $opt{'-document'});
Branch: 1 branch
BranchTrueFalseTotal
$opt{"-document"} and $self->document(delete $opt{"-document"}) 2 1 3
19  
20 3 return $self;
21 }
22  
23 3 ▶sub process {
Subroutine process: called CC=3 SCAR=11.1
Pod documented
24 3 my ($self) = @_;
25 3 ▶ my $pointer = $self->pointer // Carp::croak("'pointer' not defined");
Condition50%1/2
$self->pointer // Carp::croak("'pointer' not defined")
l!l
30
MC/DC0%0/1
$self->pointer // Carp::croak("'pointer' not defined")
$self->pointer
Truth table50%1/2
A
A= $self->pointer
Aresult
11
00
26  
27 3 for my $key (keys %$pointer) {
28 5 $self->_recursive_process($self->document, $key, $pointer->{$key});
29 }
30 3 return 1;
31 }
32  
33 11 ▶sub _recursive_process {
Subroutine _recursive_process: called CC=10 SCAR=23.2
34 11 my ($self, $document, $pointer, $cb) = @_;
35  
36 11 ▶ if ($pointer =~ /(.*?)\/\*(.+)?/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pointer =~ m[(.*?)/\*(.+)?]) { } 3 8 11
elsif ($pointer eq '') { } 1 7 8
37 3 my $path = $1;
38 3 my $tail = $2;
39 3 my $arr_ref = JSON::Pointer->get($document, $path);
40 3 ▶ if (ref($arr_ref) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arr_ref ne "ARRAY") 0 3 3
41 0 Carp::croak("Path '$path' not array");
42 }
43  
44 3 my @arr = @$arr_ref;
45 3 ▶ if ($tail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail) { } 2 1 3
46 2 for my $el (@arr) {
47 6 $self->_recursive_process($el, $tail, $cb);
48 }
49 }
50 else {
51 1 my ($root_pointer, $field_name) = $pointer =~ /(.*)\/(.+)/;
52 1 for my $el (@arr) {
53 3 $cb->($el, JSON::Pointer->get($document, $root_pointer), $field_name);
54 }
55 }
56 }
57 elsif ($pointer eq '') {
58 1 my $path = '';
59 1 my $arr_ref = JSON::Pointer->get($document, $path);
60 1 ▶ if (ref($arr_ref) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arr_ref ne "ARRAY") 0 1 1
61 0 Carp::croak("Path '$path' not array");
62 }
63  
64 1 my @arr = @$arr_ref;
65 1 for my $el (@arr) {
66 3 $cb->($el, $arr_ref, undef);
67 }
68 }
69 else {
70 7 my ($root_pointer, $field_name) = $pointer =~ /(.+)?\/(.+)/;
71 7 ▶ $cb->(JSON::Pointer->get($document, $pointer), $root_pointer ? JSON::Pointer->get($document, $root_pointer) : $document, $field_name);
Branch: 1 branch
BranchTrueFalseTotal
$root_pointer ? : 1 6 7
72 }
73 }
74  
75 ############################### GET/SET METHODS ##############################
76  
77 8 ▶sub document {
Subroutine document: called CC=4 SCAR=14.3
Pod documented
78 8 ▶ if (scalar(@_) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 3 5 8
79 3 my $ref = ref($_[1]);
80 3 ▶ if ($ref ne 'HASH' && $ref ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "HASH" and $ref ne "ARRAY") 0 3 3
Condition66%2/3
$ref ne "HASH" and $ref ne "ARRAY"
!ll&&!rl&&r
120
MC/DC0%0/2
$ref ne "HASH" and $ref ne "ARRAY"
$ref ne "HASH" $ref ne "ARRAY"
Truth table66%2/3
A and B
A= $ref ne "HASH"B= $ref ne "ARRAY"
ABresult
0X0
100
111
81 0 Carp::croak("'document' must be a hashref or arrayref");
82 }
83 3 $_[0]->{'document'} = $_[1];
84 }
85 8 return $_[0]->{'document'};
86 }
87  
88 6 ▶sub pointer {
Subroutine pointer: called CC=3 SCAR=11.3
Pod documented
89 6 ▶ if (scalar(@_) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 3 3 6
90 3 ▶ if (ref($_[1]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] ne "HASH") 0 3 3
91 0 Carp::croak("'pointer' must be a hashref");
92 }
93 3 $_[0]->{'pointer'} = $_[1];
94 }
95 6 return $_[0]->{'pointer'};
96 }
97  
98 1 1;
99  
100 __END__
↑ Summary