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             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-05-06'; # DATE
5             our $DIST = 'Role-TinyCommons-Tree'; # DIST
6             our $VERSION = '0.126'; # VERSION
7              
8 3     3   61976 use strict;
  3         14  
  3         1290  
9             our $GET_PARENT_METHOD = 'parent';
10             our $GET_CHILDREN_METHOD = 'children';
11             our $SET_PARENT_METHOD = 'parent';
12             our $SET_CHILDREN_METHOD = 'children';
13              
14             sub new_from_struct {
15 45     45 0 3128 my $class = shift;
16 45         54 my $struct = shift;
17              
18 45   66     114 my $wanted_class = $struct->{_class} || $class;
19              
20             # options that will be passed to children nodes (although children nodes can
21             # override these with their own)
22 45         81 my $pass_attributes = $struct->{_pass_attributes};
23 45 100       106 $pass_attributes = 'hash' if !defined $pass_attributes;
24 45   100     76 my $constructor = $struct->{_constructor} || "new";
25 45         58 my $instantiate = $struct->{_instantiate};
26              
27 45         106 my %attrs = map { $_ => $struct->{$_} } grep {!/^_/} keys %$struct;
  45         113  
  252         554  
28              
29 45         62 my $node;
30 45 50       103 if ($instantiate) {
31 0         0 $node = $instantiate->($wanted_class, \%attrs);
32             } else {
33 45 100       71 if (!$pass_attributes) {
    50          
    0          
34 40         434 $node = $wanted_class->$constructor;
35 40         435 for (keys %attrs) {
36 40         97 $node->$_($attrs{$_});
37             }
38             } elsif ($pass_attributes eq 'hash') {
39 5         18 $node = $wanted_class->$constructor(%attrs);
40             } elsif ($pass_attributes eq 'hashref') {
41 0         0 $node = $wanted_class->$constructor(\%attrs);
42             } else {
43 0         0 die "Invalid _pass_attributes value '$pass_attributes'";
44             }
45             }
46              
47             # connect node to parent
48 45 100       259 $node->$SET_PARENT_METHOD($struct->{_parent}) if $struct->{_parent};
49              
50             # create children
51 45 100       179 if ($struct->{_children}) {
52 19         27 my @children;
53 19         23 for my $child_struct (@{ $struct->{_children} }) {
  19         36  
54 40         170 push @children, new_from_struct(
55             $class,
56             {
57             # default for children nodes
58             _constructor => $constructor,
59             _pass_attributes => $pass_attributes,
60             _instantiate => $instantiate,
61              
62             %$child_struct,
63              
64             _parent => $node,
65             },
66             );
67             }
68             # connect node to children
69 19         63 $node->$SET_CHILDREN_METHOD(\@children);
70             }
71              
72 45         204 $node;
73             }
74              
75             1;
76             # ABSTRACT: Routine to build tree object from data structure
77              
78             __END__