| 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
MC/DC0%0/2
$struct->{'_class'} || $class
$struct->{'_class'}
$class
Truth table66%2/3
A || B
A= $struct->{'_class'}B= $class
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 25 | 45 | ▶ | my $constructor = $struct->{_constructor} || "new"; | ||||||||||||||||||
|
Condition100%2/2
$struct->{'_constructor'} || 'new'
MC/DC100%1/1
$struct->{'_constructor'} || 'new'
$struct->{'_constructor'}
Truth table100%2/2
A
A= $struct->{'_constructor'}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 32 | 0 | $node = $instantiate->($wanted_class, \%attrs); | |||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||
| 34 | 45 | ▶ | if (!$pass_attributes) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # create children | ||||||||||||||||||||
| 52 | 45 | ▶ | if ($struct->{_children}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||