File Coverage

blib/lib/Tree/Visualize.pm
Criterion Covered Total %
statement 35 36 97.2
branch 3 6 50.0
condition 5 14 35.7
subroutine 9 9 100.0
pod 2 2 100.0
total 54 67 80.6


line stmt bran cond sub pod time code
1              
2             package Tree::Visualize;
3              
4 6     6   174408 use strict;
  6         16  
  6         238  
5 6     6   36 use warnings;
  6         13  
  6         241  
6            
7             our $VERSION = '0.01';
8              
9 6     6   3183 use Tree::Visualize::Exceptions;
  6         19  
  6         192  
10 6     6   3602 use Tree::Visualize::Config;
  6         14  
  6         157  
11 6     6   3320 use Tree::Visualize::Layout::Factory;
  6         17  
  6         2315  
12              
13             sub new {
14 1     1 1 13579 my ($_class, $tree, $output_format, $layout) = @_;
15 1   33     9 my $class = ref($_class) || $_class;
16 1         2 my $visulization = {};
17 1         2 bless($visulization, $class);
18 1         4 $visulization->_init($tree, $output_format, $layout);
19 1         4 return $visulization;
20             }
21              
22             sub _init {
23 1     1   2 my ($self, $tree, $output_format, $layout) = @_;
24 1 50 33     11 (defined($tree) && ref($tree) && defined($output_format))
      33        
25             || throw Tree::Visualize::InsufficientArguments "bad arguments for Tree::Visualize->new";
26            
27 1         5 $self->{tree} = $tree;
28 1         3 $self->{tree_type} = $Tree::Visualize::Config::TREE_TYPES{ref($tree)};
29 1         3 $self->{output_format} = $output_format;
30 1         2 $self->{layout} = $layout;
31             }
32              
33             sub draw {
34 1     1 1 485 my ($self) = @_;
35 1         2 my $output;
36 1         1 eval {
37 1         9 $output = $self->_getVisualizationInstance()->draw($self->{tree});
38             };
39 1 50       25 throw Tree::Visualize::Exception ("Visualization for tree (" . $self->{tree} . ") failed", $@) if $@;
40 1 50 33     24 return $output->getAsString() if ref($output) && UNIVERSAL::isa($output, 'Tree::Visualize::ASCII::BoundingBox');
41 0         0 return $output;
42             }
43              
44              
45             sub _getVisualizationInstance {
46 1     1   2 my ($self) = @_;
47 1   50     14 return Tree::Visualize::Layout::Factory->new()->get(
48             output => $self->{output_format},
49             tree_type => $self->{tree_type},
50             layout => $self->{layout} || "TopDown"
51             );
52             }
53              
54             1;
55              
56             __END__