| 1 | package Tree::Simple::Visitor::FromNestedArray; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 13 | 4 | my ($_class) = @_; | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||||
| 15 | 4 | my $visitor = {}; | |||||||||||||||||||||||||||||
| 16 | 4 | bless($visitor, $class); | |||||||||||||||||||||||||||||
| 17 | 4 | $visitor->_init(); | |||||||||||||||||||||||||||||
| 18 | 4 | return $visitor; | |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 23 | 4 | $self->{array_tree} = undef; | |||||||||||||||||||||||||||||
| 24 | 4 | $self->SUPER::_init(); | |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 9 | ▶ | sub setArrayTree { | ||||||||||||||||||||||||||||
|
Subroutine
setArrayTree: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||
| 28 | 9 | my ($self, $array_tree) = @_; | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | (defined($array_tree) && ref($array_tree) eq 'ARRAY') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $array_tree and ref $array_tree eq "ARRAY"
MC/DC100%2/2
defined $array_tree and ref $array_tree eq "ARRAY"
defined $array_tree
ref $array_tree eq "ARRAY"
Truth table100%3/3
A and B
A= defined $array_treeB= 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 | ▶ | (scalar @{$array_tree} != 0) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 34 | || die "Insufficient Arguments : The array tree provided is empty"; | ||||||||||||||||||||||||||||||
| 35 | # it's first element must not be an array | ||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | (ref($array_tree->[0]) ne 'ARRAY') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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 | ▶ | (ref($array_tree->[1]) eq 'ARRAY') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
defined $array_tree->[1] && (ref $array_tree->[1] eq 'ARRAY' || die('Incorrect Object Type : The second value in the array tree must be an array reference'))
ref $array_tree->[1] eq "ARRAY" or die "Incorrect Object Type : The second value in the array tree must be an array reference"
MC/DC0%0/2
defined $array_tree->[1] && (ref $array_tree->[1] eq 'ARRAY' || die('Incorrect Object Type : The second value in the array tree must be an array reference'))
defined $array_tree->[1]
ref $array_tree->[1] eq "ARRAY"
Truth table33%1/3
A && B
A= defined $array_tree->[1]B= 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 | $self->{array_tree} = $array_tree; | |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | 8 | ▶ | sub visit { | ||||||||||||||||||||||||||||
|
Subroutine
visit: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 46 | 8 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||||
| 47 | 8 | ▶ | (blessed($tree) && $tree->isa("Tree::Simple")) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree)
$tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $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 | $self->getNodeFilter(), | |||||||||||||||||||||||||||||
| 55 | # pass the value of includeTrunk too | ||||||||||||||||||||||||||||||
| 56 | $self->includeTrunk() | ||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | 11 | ▶ | sub _buildTree { | ||||||||||||||||||||||||||||
|
Subroutine
_buildTree: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 61 | 11 | my ($self, $tree, $array, $node_filter, $include_trunk) = @_; | |||||||||||||||||||||||||||||
| 62 | 11 | my $i = 0; | |||||||||||||||||||||||||||||
| 63 | 11 | ▶ | while ($i < scalar @{$array}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 64 | 17 | my $node = $array->[$i]; | |||||||||||||||||||||||||||||
| 65 | # check to make sure we have a well formed tree | ||||||||||||||||||||||||||||||
| 66 | 17 | ▶ | (ref($node) ne 'ARRAY') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 67 | || die "Incorrect Object Type : The node value should never be an array reference"; | ||||||||||||||||||||||||||||||
| 68 | # filter the node if necessary | ||||||||||||||||||||||||||||||
| 69 | 16 | ▶ | $node = $node_filter->($node) if defined($node_filter); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 70 | # create the new tree | ||||||||||||||||||||||||||||||
| 71 | 16 | my $new_tree; | |||||||||||||||||||||||||||||
| 72 | 16 | ▶ | if ($include_trunk) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 73 | 1 | $tree->setNodeValue($node); | |||||||||||||||||||||||||||||
| 74 | 1 | $new_tree = $tree; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||||
| 77 | 15 | $new_tree = Tree::Simple->new($node); | |||||||||||||||||||||||||||||
| 78 | 15 | $tree->addChild($new_tree); | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||
| 80 | # increment the index value | ||||||||||||||||||||||||||||||
| 81 | 16 | $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 | ▶ | $self->_buildTree($new_tree, $array->[$i++], $node_filter) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | if ref($array->[$i]) eq 'ARRAY'; | ||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 1 | 1; | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||||