Code/Includable/Tree/FromStruct.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 71.4% 10 / 14 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 2 / 2 Podpod 0.0% 0 / 1 totaltotal 83.0% 54 / 65 CC 12 SCAR 24.7
CC
12
Cov
86%
new_from_struct
12.7
BEGIN
1.0
CRAP 11.8
↑ Summary Code/Includable/Tree/FromObjArray.pm »
1 package Code::Includable::Tree::FromStruct;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 5 our $DATE = '2021-10-07'; # DATE
7 5 our $DIST = 'RoleBundle-TinyCommons-Tree'; # DIST
8 5 our $VERSION = '0.129'; # VERSION
9  
10 5 our $GET_PARENT_METHOD = 'parent';
11 5 our $GET_CHILDREN_METHOD = 'children';
12 5 our $SET_PARENT_METHOD = 'parent';
13 5 our $SET_CHILDREN_METHOD = 'children';
14  
15 45 ▶sub new_from_struct {
Subroutine new_from_struct: called CC=12 SCAR=25.4
Pod undocumented
16 45 my $class = shift;
17 45 my $struct = shift;
18  
19 45 ▶ my $wanted_class = $struct->{_class} || $class;
Condition66%2/3
$struct->{'_class'} || $class
l!l&&r!l&&!r
20250
MC/DC0%0/2
$struct->{'_class'} || $class
$struct->{'_class'} $class
Truth table66%2/3
A || B
A= $struct->{'_class'}B= $class
ABresult
1X1
011
000
20  
21 # options that will be passed to children nodes (although children nodes can
22 # override these with their own)
23 45 my $pass_attributes = $struct->{_pass_attributes};
24 45 ▶ $pass_attributes = 'hash' if !defined $pass_attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass_attributes 1 44 45
25 45 ▶ my $constructor = $struct->{_constructor} || "new";
Condition100%2/2
$struct->{'_constructor'} || 'new'
l!l
441
MC/DC100%1/1
$struct->{'_constructor'} || 'new'
$struct->{'_constructor'}
Truth table100%2/2
A
A= $struct->{'_constructor'}
Aresult
11
00
26 45 my $instantiate = $struct->{_instantiate};
27  
28 45 my %attrs = map { $_ => $struct->{$_} } grep {!/^_/} keys %$struct;
29  
30 45 my $node;
31 45 ▶ if ($instantiate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instantiate) { } 0 45 45
32 0 $node = $instantiate->($wanted_class, \%attrs);
33 } else {
34 45 ▶ if (!$pass_attributes) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $pass_attributes) { } 40 5 45
elsif ($pass_attributes eq 'hash') { } 5 0 5
elsif ($pass_attributes eq 'hashref') { } 0 0 0
35 40 $node = $wanted_class->$constructor;
36 40 for (keys %attrs) {
37 40 $node->$_($attrs{$_});
38 }
39 } elsif ($pass_attributes eq 'hash') {
40 5 $node = $wanted_class->$constructor(%attrs);
41 } elsif ($pass_attributes eq 'hashref') {
42 0 $node = $wanted_class->$constructor(\%attrs);
43 } else {
44 0 die "Invalid _pass_attributes value '$pass_attributes'";
45 }
46 }
47  
48 # connect node to parent
49 45 ▶ $node->$SET_PARENT_METHOD($struct->{_parent}) if $struct->{_parent};
Branch: 1 branch
BranchTrueFalseTotal
if $struct->{"_parent"} 40 5 45
50  
51 # create children
52 45 ▶ if ($struct->{_children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($struct->{"_children"}) 19 26 45
53 19 my @children;
54 19 for my $child_struct (@{ $struct->{_children} }) {
55 40 push @children, new_from_struct(
56 $class,
57 {
58 # default for children nodes
59 _constructor => $constructor,
60 _pass_attributes => $pass_attributes,
61 _instantiate => $instantiate,
62  
63 %$child_struct,
64  
65 _parent => $node,
66 },
67 );
68 }
69 # connect node to children
70 19 $node->$SET_CHILDREN_METHOD(\@children);
71 }
72  
73 45 $node;
74 }
75  
76 5 1;
77 # ABSTRACT: Routine to build tree object from data structure
78  
79 __END__
↑ Summary Code/Includable/Tree/FromObjArray.pm »