File Coverage

blib/lib/Code/Includable/Tree/FromStruct.pm
Criterion Covered Total %
statement 28 31 90.3
branch 10 14 71.4
condition 4 5 80.0
subroutine 2 2 100.0
pod 0 1 0.0
total 44 53 83.0


line stmt bran cond sub pod time code
1             package Code::Includable::Tree::FromStruct;
2              
3 3     3   77312 use strict;
  3         19  
  3         1503  
4              
5             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6             our $DATE = '2021-10-07'; # DATE
7             our $DIST = 'Role-TinyCommons-Tree'; # DIST
8             our $VERSION = '0.128'; # VERSION
9              
10             our $GET_PARENT_METHOD = 'parent';
11             our $GET_CHILDREN_METHOD = 'children';
12             our $SET_PARENT_METHOD = 'parent';
13             our $SET_CHILDREN_METHOD = 'children';
14              
15             sub new_from_struct {
16 45     45 0 3153 my $class = shift;
17 45         68 my $struct = shift;
18              
19 45   66     135 my $wanted_class = $struct->{_class} || $class;
20              
21             # options that will be passed to children nodes (although children nodes can
22             # override these with their own)
23 45         71 my $pass_attributes = $struct->{_pass_attributes};
24 45 100       120 $pass_attributes = 'hash' if !defined $pass_attributes;
25 45   100     93 my $constructor = $struct->{_constructor} || "new";
26 45         74 my $instantiate = $struct->{_instantiate};
27              
28 45         121 my %attrs = map { $_ => $struct->{$_} } grep {!/^_/} keys %$struct;
  45         139  
  252         618  
29              
30 45         85 my $node;
31 45 50       91 if ($instantiate) {
32 0         0 $node = $instantiate->($wanted_class, \%attrs);
33             } else {
34 45 100       80 if (!$pass_attributes) {
    50          
    0          
35 40         474 $node = $wanted_class->$constructor;
36 40         535 for (keys %attrs) {
37 40         124 $node->$_($attrs{$_});
38             }
39             } elsif ($pass_attributes eq 'hash') {
40 5         22 $node = $wanted_class->$constructor(%attrs);
41             } elsif ($pass_attributes eq 'hashref') {
42 0         0 $node = $wanted_class->$constructor(\%attrs);
43             } else {
44 0         0 die "Invalid _pass_attributes value '$pass_attributes'";
45             }
46             }
47              
48             # connect node to parent
49 45 100       319 $node->$SET_PARENT_METHOD($struct->{_parent}) if $struct->{_parent};
50              
51             # create children
52 45 100       186 if ($struct->{_children}) {
53 19         30 my @children;
54 19         33 for my $child_struct (@{ $struct->{_children} }) {
  19         39  
55 40         214 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         60 $node->$SET_CHILDREN_METHOD(\@children);
71             }
72              
73 45         216 $node;
74             }
75              
76             1;
77             # ABSTRACT: Routine to build tree object from data structure
78              
79             __END__