File Coverage

blib/lib/Tree/Simple/Visitor/FromNestedArray.pm
Criterion Covered Total %
statement 46 46 100.0
branch 17 18 94.4
condition 9 11 81.8
subroutine 9 9 100.0
pod 3 3 100.0
total 84 87 96.5


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::FromNestedArray;
2              
3 1     1   61015 use strict;
  1         12  
  1         24  
4 1     1   5 use warnings;
  1         1  
  1         34  
5              
6             our $VERSION = '0.16';
7              
8 1     1   4 use Scalar::Util qw(blessed);
  1         2  
  1         55  
9              
10 1     1   6 use base qw(Tree::Simple::Visitor);
  1         1  
  1         441  
11              
12             sub new {
13 4     4 1 10962 my ($_class) = @_;
14 4   33     20 my $class = ref($_class) || $_class;
15 4         8 my $visitor = {};
16 4         9 bless($visitor, $class);
17 4         9 $visitor->_init();
18 4         33 return $visitor;
19             }
20              
21             sub _init {
22 4     4   7 my ($self) = @_;
23 4         9 $self->{array_tree} = undef;
24 4         13 $self->SUPER::_init();
25             }
26              
27             sub setArrayTree {
28 9     9 1 4279 my ($self, $array_tree) = @_;
29 9 100 100     51 (defined($array_tree) && ref($array_tree) eq 'ARRAY')
30             || die "Insufficient Arguments : You must supply a valid ARRAY reference";
31             # validate the tree ...
32             # it must not be empty
33 7 100       10 (scalar @{$array_tree} != 0)
  7         23  
34             || die "Insufficient Arguments : The array tree provided is empty";
35             # it's first element must not be an array
36 6 100       22 (ref($array_tree->[0]) ne 'ARRAY')
37             || die "Incorrect Object Type : The first value in the array tree is an array reference";
38             # and it must be a single rooted tree
39 5 50 100     25 (ref($array_tree->[1]) eq 'ARRAY')
40             || die "Incorrect Object Type : The second value in the array tree must be an array reference"
41             if defined($array_tree->[1]);
42 4         8 $self->{array_tree} = $array_tree;
43             }
44              
45             sub visit {
46 8     8 1 2828 my ($self, $tree) = @_;
47 8 100 100     76 (blessed($tree) && $tree->isa("Tree::Simple"))
48             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
49             $self->_buildTree(
50             $tree,
51             # our array tree
52             $self->{array_tree},
53             # get a node filter if we have one
54 4         18 $self->getNodeFilter(),
55             # pass the value of includeTrunk too
56             $self->includeTrunk()
57             );
58             }
59              
60             sub _buildTree {
61 11     11   69 my ($self, $tree, $array, $node_filter, $include_trunk) = @_;
62 11         14 my $i = 0;
63 11         17 while ($i < scalar @{$array}) {
  26         55  
64 17         43 my $node = $array->[$i];
65             # check to make sure we have a well formed tree
66 17 100       41 (ref($node) ne 'ARRAY')
67             || die "Incorrect Object Type : The node value should never be an array reference";
68             # filter the node if necessary
69 16 100       32 $node = $node_filter->($node) if defined($node_filter);
70             # create the new tree
71 16         38 my $new_tree;
72 16 100       24 if ($include_trunk) {
73 1         5 $tree->setNodeValue($node);
74 1         7 $new_tree = $tree;
75             }
76             else {
77 15         31 $new_tree = Tree::Simple->new($node);
78 15         415 $tree->addChild($new_tree);
79             }
80             # increment the index value
81 16         1114 $i++;
82             # NOTE:
83             # the value of include trunk is only
84             # passed in the recursion, so that
85             # the trunk/root can be populated,
86             # we have no more need for it after
87             # that time.
88 16 100       56 $self->_buildTree($new_tree, $array->[$i++], $node_filter)
89             if ref($array->[$i]) eq 'ARRAY';
90             }
91             }
92              
93             1;
94              
95             __END__