File Coverage

blib/lib/Tree/Simple/Visitor/LoadClassHierarchy.pm
Criterion Covered Total %
statement 68 68 100.0
branch 19 26 73.0
condition 3 9 33.3
subroutine 13 13 100.0
pod 4 4 100.0
total 107 120 89.1


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::LoadClassHierarchy;
2              
3 1     1   16834 use strict;
  1         2  
  1         23  
4 1     1   3 use warnings;
  1         2  
  1         32  
5              
6             our $VERSION = '0.15';
7              
8 1     1   4 use Scalar::Util qw(blessed);
  1         1  
  1         92  
9              
10 1     1   4 use base qw(Tree::Simple::Visitor);
  1         1  
  1         424  
11              
12             sub new {
13 4     4 1 8610 my ($_class) = @_;
14 4   33     21 my $class = ref($_class) || $_class;
15 4         6 my $visitor = {};
16 4         7 bless($visitor, $class);
17 4         9 $visitor->_init();
18 4         26 return $visitor;
19             }
20              
21             sub _init {
22 4     4   5 my ($self) = @_;
23 4         12 $self->{class_to_load} = undef;
24 4         5 $self->{include_methods} = 0;
25 4         18 $self->SUPER::_init();
26             }
27              
28             sub setClass {
29 4     4 1 4579 my ($self, $class_to_load) = @_;
30 4 50       13 (defined($class_to_load)) || die "Insufficient Arguments : Must provide a class to load";
31 4         9 $self->{class_to_load} = $class_to_load;
32             }
33              
34             sub includeMethods {
35 24     24 1 700 my ($self, $boolean) = @_;
36 24 50       44 $self->{include_methods} = ($boolean ? 1 : 0) if defined $boolean;
    100          
37 24         48 return $self->{include_methods};
38             }
39              
40             sub visit {
41 4     4 1 3959 my ($self, $tree) = @_;
42 4 50 33     38 (blessed($tree) && $tree->isa("Tree::Simple"))
43             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
44             # it must be a leaf
45 4 50       12 ($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a class hierarchy";
46 4 50       44 (defined $self->{class_to_load}) || die "Insufficient Arguments : Must provide a class to load";
47             # get the filter
48 4         14 my $filter = $self->getNodeFilter();
49             # get the class to load
50 4   33     24 my $class_to_load = ref($self->{class_to_load}) || $self->{class_to_load};
51              
52             # deal with the include trunk functionality
53 4 100       15 if ($self->includeTrunk()) {
54 1 50       24 $tree->setNodeValue(defined $filter ? $filter->($class_to_load) : $class_to_load);
55             }
56             else {
57 3 100       33 my $new_tree = Tree::Simple->new(defined $filter ? $filter->($class_to_load) : $class_to_load);
58 3         74 $tree->addChild($new_tree);
59 3 100       179 if ($self->includeMethods()) {
60 1         3 $self->_loadMethods($new_tree, $class_to_load, $filter);
61             }
62 3         76 $tree = $new_tree;
63             }
64              
65             # and load it recursively
66 4         13 $self->_loadClass($tree, $class_to_load, $filter);
67             }
68              
69             sub _loadClass {
70 24     24   34 my ($self, $tree, $class_to_load, $filter) = @_;
71 24         14 my @superclasses;
72             {
73 1     1   916 no strict 'refs';
  1         1  
  1         101  
  24         20  
74 24         21 @superclasses = @{"${class_to_load}::ISA"};
  24         88  
75             }
76 24         53 foreach my $superclass (@superclasses) {
77 20 100       54 my $new_tree = Tree::Simple->new(defined $filter ? $filter->($superclass) : $superclass);
78 20         586 $tree->addChild($new_tree);
79 20 100       1203 if ($self->includeMethods()) {
80 2         4 $self->_loadMethods($new_tree, $superclass, $filter);
81             }
82 20         168 $self->_loadClass($new_tree, $superclass, $filter);
83             }
84             }
85              
86             sub _loadMethods {
87 3     3   5 my ($self, $tree, $class, $filter) = @_;
88 3         2 my @methods;
89             {
90 1     1   4 no strict 'refs';
  1         1  
  1         116  
  3         3  
91 3         2 @methods = sort grep { defined &{"${class}::$_"} } keys %{"${class}::"};
  6         3  
  6         18  
  3         8  
92             }
93 3         5 foreach my $method (@methods) {
94 4 50       77 $tree->addChild(Tree::Simple->new(defined $filter ? $filter->($method) : $method));
95             }
96             }
97              
98             1;
99              
100             __END__