File Coverage

blib/lib/Tree/Simple/Visitor/PathToRoot.pm
Criterion Covered Total %
statement 33 33 100.0
branch 7 8 87.5
condition 6 8 75.0
subroutine 8 8 100.0
pod 4 4 100.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::PathToRoot;
2              
3 2     2   30585 use strict;
  2         3  
  2         66  
4 2     2   7 use warnings;
  2         3  
  2         98  
5              
6             our $VERSION = '0.14';
7              
8 2     2   8 use Scalar::Util qw(blessed);
  2         3  
  2         223  
9              
10 2     2   10 use base qw(Tree::Simple::Visitor);
  2         10  
  2         1250  
11              
12             sub new {
13 2     2 1 2441 my ($_class) = @_;
14 2   33     14 my $class = ref($_class) || $_class;
15 2         5 my $visitor = {};
16 2         5 bless($visitor, $class);
17 2         13 $visitor->_init();
18 2         22 return $visitor;
19             }
20              
21             sub visit {
22 8     8 1 4113 my ($self, $tree) = @_;
23 8 100 100     91 (blessed($tree) && $tree->isa("Tree::Simple"))
24             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
25             # create an array for our path
26 4         4 my @path;
27             # we need to climb up the tree and
28             # collect the nodes
29 4         17 my $filter_function = $self->getNodeFilter();
30 4         13 my $current_tree = $tree;
31 4         7 until ($current_tree->isRoot()) {
32 7 100       43 unshift @path => ($filter_function ?
33             $filter_function->($current_tree)
34             :
35             $current_tree->getNodeValue());
36 7         24 $current_tree = $current_tree->getParent();
37             }
38             # now grab the trunk if specified
39 4 50       28 unshift @path => ($filter_function ?
    100          
40             $filter_function->($current_tree)
41             :
42             $current_tree->getNodeValue()) if $self->includeTrunk();
43             # now store our path in results
44 4         37 $self->setResults(@path);
45             }
46              
47             sub getPath {
48 4     4 1 1221 my ($self) = @_;
49 4         10 return $self->getResults();
50             }
51              
52             sub getPathAsString {
53 4     4 1 27 my ($self, $delimiter) = @_;
54 4   100     12 $delimiter ||= ", ";
55 4         10 return join $delimiter => $self->getResults();
56             }
57              
58             1;
59              
60             __END__