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   21217 use strict;
  1         1  
  1         22  
4 1     1   3 use warnings;
  1         2  
  1         30  
5              
6             our $VERSION = '0.15';
7              
8 1     1   3 use Scalar::Util qw(blessed);
  1         1  
  1         88  
9              
10 1     1   16 use base qw(Tree::Simple::Visitor);
  1         1  
  1         415  
11              
12             sub new {
13 4     4 1 11021 my ($_class) = @_;
14 4   33     21 my $class = ref($_class) || $_class;
15 4         7 my $visitor = {};
16 4         6 bless($visitor, $class);
17 4         8 $visitor->_init();
18 4         24 return $visitor;
19             }
20              
21             sub _init {
22 4     4   6 my ($self) = @_;
23 4         9 $self->{array_tree} = undef;
24 4         15 $self->SUPER::_init();
25             }
26              
27             sub setArrayTree {
28 9     9 1 4658 my ($self, $array_tree) = @_;
29 9 100 100     64 (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       23 (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     23 (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         7 $self->{array_tree} = $array_tree;
43             }
44              
45             sub visit {
46 8     8 1 3638 my ($self, $tree) = @_;
47 8 100 100     91 (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         14 $self->getNodeFilter(),
55             # pass the value of includeTrunk too
56             $self->includeTrunk()
57             );
58             }
59              
60             sub _buildTree {
61 11     11   38 my ($self, $tree, $array, $node_filter, $include_trunk) = @_;
62 11         6 my $i = 0;
63 11         11 while ($i < scalar @{$array}) {
  26         51  
64 17         18 my $node = $array->[$i];
65             # check to make sure we have a well formed tree
66 17 100       57 (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       25 $node = $node_filter->($node) if defined($node_filter);
70             # create the new tree
71 16         19 my $new_tree;
72 16 100       19 if ($include_trunk) {
73 1         3 $tree->setNodeValue($node);
74 1         4 $new_tree = $tree;
75             }
76             else {
77 15         26 $new_tree = Tree::Simple->new($node);
78 15         253 $tree->addChild($new_tree);
79             }
80             # increment the index value
81 16         1223 $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       49 $self->_buildTree($new_tree, $array->[$i++], $node_filter)
89             if ref($array->[$i]) eq 'ARRAY';
90             }
91             }
92              
93             1;
94              
95             __END__