| 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
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | $opt{'-document'} && $self->document(delete $opt{'-document'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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")
MC/DC0%0/1
$self->pointer // Carp::croak("'pointer' not defined")
$self->pointer
Truth table50%1/2
A
A= $self->pointer
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 0 | Carp::croak("Path '$path' not array"); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | my @arr = @$arr_ref; | |||||||||||||||||||||||||||
| 45 | 3 | ▶ | if ($tail) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 79 | 3 | my $ref = ref($_[1]); | |||||||||||||||||||||||||||
| 80 | 3 | ▶ | if ($ref ne 'HASH' && $ref ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref ne "HASH" and $ref ne "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | if (ref($_[1]) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||