File Coverage

blib/lib/Tree/FromStruct.pm
Criterion Covered Total %
statement 6 6 100.0
branch 1 2 50.0
condition n/a
subroutine 2 2 100.0
pod 1 1 100.0
total 10 11 90.9


line stmt bran cond sub pod time code
1             package Tree::FromStruct;
2              
3             our $DATE = '2016-03-29'; # DATE
4             our $VERSION = '0.03'; # VERSION
5              
6             require Code::Includable::Tree::FromStruct;
7              
8 1     1   71630 use Exporter qw(import);
  1         16  
  1         102  
9             our @EXPORT_OK = qw(build_tree_from_struct);
10              
11             sub build_tree_from_struct {
12 1     1 1 95 my $struct = shift;
13              
14 1 50       4 my $class = $struct->{_class} or die "Please specify _class in struct";
15 1         5 Code::Includable::Tree::FromStruct::new_from_struct($class, $struct);
16             }
17              
18             1;
19             # ABSTRACT: Build a tree object from hash structure
20              
21             __END__
22              
23             =pod
24              
25             =encoding UTF-8
26              
27             =head1 NAME
28              
29             Tree::FromStruct - Build a tree object from hash structure
30              
31             =head1 VERSION
32              
33             This document describes version 0.03 of Tree::FromStruct (from Perl distribution Tree-FromStruct), released on 2016-03-29.
34              
35             =head1 SYNOPSIS
36              
37             In your tree node class F<My/Person.pm>:
38              
39             package My::Person;
40              
41             sub new {
42             my $class = shift;
43             my %args = @_;
44             bless \%args, $class;
45             }
46              
47             sub parent {
48             my $self = shift;
49             $self->{_parent} = $_[0] if $@;
50             $self->{_parent};
51             }
52              
53             sub children {
54             my $self = shift;
55             $self->{_children} = $_[0] if $@;
56             $self->{_children};
57             }
58              
59             In your code to build a tree:
60              
61             use Tree::FromStruct qw(build_tree_from_struct);
62              
63             # require all the used classes
64             use My::Person;
65             use My::MarriedPerson;
66             use My::KidPerson;
67              
68             my $family_tree = build_tree_from_struct({
69             _class => 'My::Person', name => 'Andi', age => 60, _children => [
70             {name => 'Budi', age => 30},
71             {_class => 'My::MarriedPerson', name => 'Cinta', _children => [
72             {class => 'My::KidPerson', name => 'Deni'},
73             {class => 'My::KidPerson', name => 'Eno'},
74             ]},
75             ]});
76              
77             =head1 DESCRIPTION
78              
79             Building a tree manually can be tedious: you have to connect the parent and
80             the children nodes together:
81              
82             my $root = My::TreeNode->new(...);
83             my $child1 = My::TreeNode->new(...);
84             my $child2 = My::TreeNode->new(...);
85              
86             $root->children([$child1, $child2]);
87             $child1->parent($root);
88             $child2->parent($root);
89              
90             my $grandchild1 = My::TreeNode->new(...);
91             ...
92              
93             This module provides a convenience function to build a tree of objects in a
94             single command. It connect the parent and children nodes for you.
95              
96             The class can be any class that provides C<parent> and C<children> methods. See
97             L<Role::TinyCommons::Tree::Node> for more details.
98              
99             =head1 FUNCTIONS
100              
101             =head2 build_tree_from_struct($struct) => obj
102              
103             This is basically L<Role::TinyCommons::Tree::FromStruct>'s C<new_from_struct>
104             presented as a function. See the role's documentation for more details on what
105             you can put in C<$struct>.
106              
107             =head1 HOMEPAGE
108              
109             Please visit the project's homepage at L<https://metacpan.org/release/Tree-FromStruct>.
110              
111             =head1 SOURCE
112              
113             Source repository is at L<https://github.com/perlancar/perl-Tree-FromStruct>.
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Tree-FromStruct>
118              
119             When submitting a bug or request, please include a test-file or a
120             patch to an existing test-file that illustrates the bug or desired
121             feature.
122              
123             =head1 SEE ALSO
124              
125             L<Role::TinyCommons::Tree::FromStruct> if you want to use this functionality via
126             consuming a role.
127              
128             Other ways to create tree: L<Tree::FromText>, L<Tree::FromTextLines>,
129             L<Tree::Create::Callback>, L<Tree::Create::Size>.
130              
131             =head1 AUTHOR
132              
133             perlancar <perlancar@cpan.org>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is copyright (c) 2016 by perlancar@cpan.org.
138              
139             This is free software; you can redistribute it and/or modify it under
140             the same terms as the Perl 5 programming language system itself.
141              
142             =cut