2 package Tree::Simple::Visitor::LoadClassHierarchy;
9 use Scalar::Util qw(blessed);
11 use base qw(Tree::Simple::Visitor);
15 my $class = ref($_class) || $_class;
17 bless($visitor, $class);
24 $self->{class_to_load} = undef;
25 $self->{include_methods} = 0;
26 $self->SUPER::_init();
30 my ($self, $class_to_load) = @_;
31 (defined($class_to_load)) || die "Insufficient Arguments : Must provide a class to load";
32 $self->{class_to_load} = $class_to_load;
36 my ($self, $boolean) = @_;
37 $self->{include_methods} = ($boolean ? 1 : 0) if defined $boolean;
38 return $self->{include_methods};
42 my ($self, $tree) = @_;
43 (blessed($tree) && $tree->isa("Tree::Simple"))
44 || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
46 ($tree->isLeaf()) || die "Illegal Operation : The tree must be a leaf node to load a class hierarchy";
47 (defined $self->{class_to_load}) || die "Insufficient Arguments : Must provide a class to load";
49 my $filter = $self->getNodeFilter();
50 # get the class to load
51 my $class_to_load = ref($self->{class_to_load}) || $self->{class_to_load};
53 # deal with the include trunk functionality
54 if ($self->includeTrunk()) {
55 $tree->setNodeValue(defined $filter ? $filter->($class_to_load) : $class_to_load);
58 my $new_tree = Tree::Simple->new(defined $filter ? $filter->($class_to_load) : $class_to_load);
59 $tree->addChild($new_tree);
60 if ($self->includeMethods()) {
61 $self->_loadMethods($new_tree, $class_to_load, $filter);
66 # and load it recursively
67 $self->_loadClass($tree, $class_to_load, $filter);
71 my ($self, $tree, $class_to_load, $filter) = @_;
75 @superclasses = @{"${class_to_load}::ISA"};
77 foreach my $superclass (@superclasses) {
78 my $new_tree = Tree::Simple->new(defined $filter ? $filter->($superclass) : $superclass);
79 $tree->addChild($new_tree);
80 if ($self->includeMethods()) {
81 $self->_loadMethods($new_tree, $superclass, $filter);
83 $self->_loadClass($new_tree, $superclass, $filter);
88 my ($self, $tree, $class, $filter) = @_;
92 @methods = sort grep { defined &{"${class}::$_"} } keys %{"${class}::"};
94 foreach my $method (@methods) {
95 $tree->addChild(Tree::Simple->new(defined $filter ? $filter->($method) : $method));
105 Tree::Simple::Visitor::LoadClassHierarchy - A Visitor for loading class hierarchies into a Tree::Simple hierarchy
109 use Tree::Simple::Visitor::LoadClassHierarchy;
112 my $visitor = Tree::Simple::Visitor::LoadClassHierarchy->new();
114 # set class as an instance, or
115 $visitor->setClass($class);
118 $visitor->setClass("My::Class");
120 # pass our visitor to the tree
121 $tree->accept($visitor);
123 # the $tree now mirrors the inheritance hierarchy of the $class
127 This visitor will traverse a class's inheritance hierarchy (through the @ISA arrays) and create a Tree::Simple hierarchy which mirrors it.
135 There are no arguments to the constructor the object will be in its default state. You can use the C<setNodeFilter> method to customize its behavior.
137 =item B<includeTrunk ($boolean)>
139 Setting the C<$boolean> value to true (C<1>) will cause the node value of the C<$tree> object passed into C<visit> to be set with the root value found in the class heirarchy. Setting it to false (C<0>), or not setting it, will result in the first value in the class heirarchy creating a new node level.
141 =item B<includeMethods ($boolean)>
143 Setting the C<$boolean> value to true (C<1>) will cause methods to be added as a children of the class node. Setting it to false (C<0>), or not setting it, will result in this not happening.
145 B<NOTE:> Methods are sorted ascii-betically before they are added to the tree. This allows a more predictable heirarchy.
147 =item B<setClass ($class)>
149 The argument C<$class> should be either a class name or an instance, it is then used as the root from which to determine the class hierarchy.
151 =item B<setNodeFilter ($filter_function)>
153 This method accepts a CODE reference as its C<$filter_function> argument and throws an exception if it is not a code reference. This code reference is used to filter the tree nodes as they are created, the C<$filter_function> is passed the node value extracted from the hash prior to it being inserted into the tree being built. The C<$filter_function> is expected to return the value desired for inclusion into the tree.
155 =item B<visit ($tree)>
157 This is the method that is used by Tree::Simple's C<accept> method. It can also be used on its own, it requires the C<$tree> argument to be a Tree::Simple object (or derived from a Tree::Simple object), and will throw and exception otherwise.
159 The C<$tree> argument which is passed to C<visit> must be a leaf node. This is because this Visitor will create all the sub-nodes for this tree. If the tree is not a leaf, an exception is thrown. We do not require the tree to be a root though, and this Visitor will not affect any nodes above the C<$tree> argument.
167 =item Improve the C<includeMethods> functionality
169 I am not sure the tree this creates is the optimal tree for this situation. It is sufficient for now, until I have more of an I<actual> need for this functionality.
171 =item Add C<includeFullSymbolTable> functionality
173 This would traverse the full symbol tables and produce a detailed tree of everything it finds. This takes a lot more work, and as I have no current need for it, it remains in the TO DO list.
179 None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
183 See the B<CODE COVERAGE> section in L<Tree::Simple::VisitorFactory> for more inforamtion.
187 These Visitor classes are all subclasses of B<Tree::Simple::Visitor>, which can be found in the B<Tree::Simple> module, you should refer to that module for more information.
191 stevan little, E<lt>stevan@iinteractive.comE<gt>
193 =head1 COPYRIGHT AND LICENSE
195 Copyright 2004, 2005 by Infinity Interactive, Inc.
197 L<http://www.iinteractive.com>
199 This library is free software; you can redistribute it and/or modify
200 it under the same terms as Perl itself.