File Coverage

blib/lib/Tree/Simple/View.pm
Criterion Covered Total %
statement 47 47 100.0
branch 16 18 88.8
condition 6 9 66.6
subroutine 17 17 100.0
pod 11 11 100.0
total 97 102 95.1


line stmt bran cond sub pod time code
1             package Tree::Simple::View;
2              
3 8     8   65027 use strict;
  8         23  
  8         194  
4 8     8   35 use warnings;
  8         13  
  8         309  
5              
6             our $VERSION = '0.20';
7              
8 8     8   39 use Scalar::Util qw(blessed);
  8         14  
  8         538  
9              
10 8     8   2274 use Tree::Simple::View::Exceptions;
  8         19  
  8         4605  
11              
12             sub new {
13 58     58 1 53916 my ($_class, $tree, %configuration) = @_;
14 58   33     281 my $class = ref($_class) || $_class;
15 58 100       158 ($class ne 'Tree::Simple::View')
16             || throw Tree::Simple::View::AbstractClass "Tree::Simple::View is an abstract class, try Tree::Simple::View::HTML or Tree::Simple::View::DHTML instead";
17 57         265 my $tree_view = bless{
18             tree => undef,
19             config => {},
20             include_trunk => undef,
21             ppath_comparison_func => undef
22             } => $class;
23 57         248 $tree_view->_init($tree, %configuration);
24 53         148 return $tree_view;
25             }
26              
27             sub _init {
28 57     57   142 my ($self, $tree, %config) = @_;
29 57 100 100     424 (blessed($tree) && $tree->isa("Tree::Simple"))
30             || throw Tree::Simple::View::InsufficientArguments "tree argument must be a Tree::Simple object";
31 53         129 $self->{tree} = $tree;
32 53 100       129 $self->{config} = \%config if %config;
33 53         79 $self->{include_trunk} = 0;
34 53         114 $self->{path_comparison_func} = undef;
35             }
36              
37 1     1 1 808 sub getTree { (shift)->{tree} }
38 1     1 1 6 sub getConfig { (shift)->{config} }
39              
40             sub includeTrunk {
41 15     15 1 4555 my ($self, $boolean) = @_;
42 15 50       84 $self->{include_trunk} = ($boolean ? 1 : 0) if defined $boolean;
    50          
43 15         31 return $self->{include_trunk};
44             }
45              
46             sub setPathComparisonFunction {
47 4     4 1 932 my ($self, $code) = @_;
48 4 100 66     32 (defined($code) && ref($code) eq "CODE")
49             || throw Tree::Simple::View::InsufficientArguments "Path comparison must be a function";
50 3         9 $self->{path_comparison_func} = $code;
51             }
52              
53             sub expandPath {
54 27     27 1 4997 my ($self, @path) = @_;
55 27 100       44 return $self->expandPathComplex($self->{tree}, $self->{config}, @path) if (keys %{$self->{config}});
  27         151  
56 9         48 return $self->expandPathSimple($self->{tree}, @path);
57             }
58              
59             # override these method
60 2     2 1 44 sub expandPathSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
61 2     2 1 35 sub expandPathComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
62              
63             sub expandAll {
64 28     28 1 5745 my ($self) = @_;
65 28 100       50 return $self->expandAllComplex($self->{config}) if (keys %{$self->{config}});
  28         163  
66 7         30 return $self->expandAllSimple();
67             }
68              
69             # override these method
70 2     2 1 39 sub expandAllSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
71 2     2 1 38 sub expandAllComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
72              
73             ## private methods
74              
75             sub _compareNodeToPath {
76 117     117   189 my ($self, $current_path, $current_tree) = @_;
77             # default to normal node-path comparison ...
78             return $current_path eq $current_tree->getNodeValue()
79 117 100       275 unless defined $self->{path_comparison_func};
80             # unless we have a path_comparison_func in place
81             # in which case we use that
82 11         28 return $self->{path_comparison_func}->($current_path, $current_tree);
83             }
84              
85             1;
86              
87             __END__