| 1 | package Code::Includable::Tree::FromObjArray; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $DATE = '2021-10-07'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | our $DIST = 'RoleBundle-TinyCommons-Tree'; # DIST | |||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.129'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | our $GET_PARENT_METHOD = 'parent'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our $GET_CHILDREN_METHOD = 'children'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | our $SET_PARENT_METHOD = 'parent'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | our $SET_CHILDREN_METHOD = 'children'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub __build_subtree { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__build_subtree: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | my ($parent_node, @obj_array) = ($_[0], @{$_[1] // []}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] // []
MC/DC0%0/1
$_[1] // []
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my @children; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | while (@obj_array) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | my $child_node = shift @obj_array; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # connect child node to its parent | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | $child_node->$SET_PARENT_METHOD($parent_node); | |||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | push @children, $child_node; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # the child has its own children, recurse | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | if (@obj_array && ref $obj_array[0] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@obj_array and ref $obj_array[0] eq "ARRAY"
MC/DC50%1/2
@obj_array and ref $obj_array[0] eq "ARRAY"
@obj_array
ref $obj_array[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @obj_arrayB= ref $obj_array[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | __build_subtree($child_node, shift(@obj_array)); | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # connect parent node to its children | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 3 | $parent_node->$SET_CHILDREN_METHOD(\@children); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # return something useful | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | $parent_node; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | sub new_from_obj_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_from_obj_array: called CC=4 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my $obj_array = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | die "Object array must be a one- or two-element array" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
ref $obj_array eq "ARRAY" and @$obj_array == 1 || @$obj_array == 2
@$obj_array == 1 or @$obj_array == 2
MC/DC0%0/3
ref $obj_array eq "ARRAY" and @$obj_array == 1 || @$obj_array == 2
ref $obj_array eq "ARRAY"
@$obj_array == 1
@$obj_array == 2
Truth table25%1/4
A and B or C
A= ref $obj_array eq "ARRAY"B= @$obj_array == 1C= @$obj_array == 2
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | unless ref $obj_array eq 'ARRAY' && (@$obj_array == 1 || @$obj_array == 2); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | __build_subtree(@$obj_array); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 49 | # ABSTRACT: Routine to build a tree of objects from a nested array of objects | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | __END__ | ||||||||||||||||||||||||||||||||||||||||||