|  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-07-02'; # DATE  | 
| 
5
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our $DIST = 'Role-TinyCommons-Tree'; # DIST  | 
| 
6
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our $VERSION = '0.127'; # VERSION  | 
| 
7
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
8
 | 
3
 | 
 
 | 
 
 | 
  
3
  
 | 
 
 | 
65426
 | 
 use strict;  | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
12
 | 
    | 
| 
 
 | 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1087
 | 
    | 
| 
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
  
 | 
2557
 | 
     my $class = shift;  | 
| 
16
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
58
 | 
     my $struct = shift;  | 
| 
17
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
18
 | 
45
 | 
 
 | 
  
 66
  
 | 
 
 | 
 
 | 
120
 | 
     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
 | 
 
 | 
 
 | 
 
 | 
 
 | 
64
 | 
     my $pass_attributes = $struct->{_pass_attributes};  | 
| 
23
 | 
45
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
92
 | 
     $pass_attributes = 'hash' if !defined $pass_attributes;  | 
| 
24
 | 
45
 | 
 
 | 
  
100
  
 | 
 
 | 
 
 | 
92
 | 
     my $constructor = $struct->{_constructor} || "new";  | 
| 
25
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
60
 | 
     my $instantiate = $struct->{_instantiate};  | 
| 
26
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
27
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
114
 | 
     my %attrs = map { $_ => $struct->{$_} } grep {!/^_/} keys %$struct;  | 
| 
 
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
114
 | 
    | 
| 
 
 | 
252
 | 
 
 | 
 
 | 
 
 | 
 
 | 
554
 | 
    | 
| 
28
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
29
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
74
 | 
     my $node;  | 
| 
30
 | 
45
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
76
 | 
     if ($instantiate) {  | 
| 
31
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         $node = $instantiate->($wanted_class, \%attrs);  | 
| 
32
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     } else {  | 
| 
33
 | 
45
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
77
 | 
         if (!$pass_attributes) {  | 
| 
 
 | 
 
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
 
 | 
 
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
34
 | 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
454
 | 
             $node = $wanted_class->$constructor;  | 
| 
35
 | 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
487
 | 
             for (keys %attrs) {  | 
| 
36
 | 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
113
 | 
                 $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
  
 | 
 
 | 
 
 | 
 
 | 
276
 | 
     $node->$SET_PARENT_METHOD($struct->{_parent}) if $struct->{_parent};  | 
| 
49
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
50
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     # create children  | 
| 
51
 | 
45
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
155
 | 
     if ($struct->{_children}) {  | 
| 
52
 | 
19
 | 
 
 | 
 
 | 
 
 | 
 
 | 
26
 | 
         my @children;  | 
| 
53
 | 
19
 | 
 
 | 
 
 | 
 
 | 
 
 | 
26
 | 
         for my $child_struct (@{ $struct->{_children} }) {  | 
| 
 
 | 
19
 | 
 
 | 
 
 | 
 
 | 
 
 | 
37
 | 
    | 
| 
54
 | 
40
 | 
 
 | 
 
 | 
 
 | 
 
 | 
171
 | 
             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
 | 
 
 | 
 
 | 
 
 | 
 
 | 
56
 | 
         $node->$SET_CHILDREN_METHOD(\@children);  | 
| 
70
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
     }  | 
| 
71
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
72
 | 
45
 | 
 
 | 
 
 | 
 
 | 
 
 | 
197
 | 
     $node;  | 
| 
73
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
74
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
75
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 1;  | 
| 
76
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 # ABSTRACT: Routine to build tree object from data structure  | 
| 
77
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
78
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 __END__  |