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 7     7   24553 use strict;
  7         10  
  7         167  
4 7     7   23 use warnings;
  7         18  
  7         234  
5              
6             our $VERSION = '0.19';
7              
8 7     7   30 use Scalar::Util qw(blessed);
  7         10  
  7         438  
9              
10 7     7   1665 use Tree::Simple::View::Exceptions;
  7         10  
  7         3186  
11              
12             sub new {
13 58     58 1 41228 my ($_class, $tree, %configuration) = @_;
14 58   33     214 my $class = ref($_class) || $_class;
15 58 100       117 ($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         239 my $tree_view = bless{
18             tree => undef,
19             config => {},
20             include_trunk => undef,
21             ppath_comparison_func => undef
22             } => $class;
23 57         175 $tree_view->_init($tree, %configuration);
24 53         117 return $tree_view;
25             }
26              
27             sub _init {
28 57     57   85 my ($self, $tree, %config) = @_;
29 57 100 100     373 (blessed($tree) && $tree->isa("Tree::Simple"))
30             || throw Tree::Simple::View::InsufficientArguments "tree argument must be a Tree::Simple object";
31 53         88 $self->{tree} = $tree;
32 53 100       98 $self->{config} = \%config if %config;
33 53         52 $self->{include_trunk} = 0;
34 53         78 $self->{path_comparison_func} = undef;
35             }
36              
37 1     1 1 611 sub getTree { (shift)->{tree} }
38 1     1 1 4 sub getConfig { (shift)->{config} }
39              
40             sub includeTrunk {
41 15     15 1 4064 my ($self, $boolean) = @_;
42 15 50       63 $self->{include_trunk} = ($boolean ? 1 : 0) if defined $boolean;
    50          
43 15         21 return $self->{include_trunk};
44             }
45              
46             sub setPathComparisonFunction {
47 4     4 1 973 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         7 $self->{path_comparison_func} = $code;
51             }
52              
53             sub expandPath {
54 27     27 1 4428 my ($self, @path) = @_;
55 27 100       24 return $self->expandPathComplex($self->{tree}, $self->{config}, @path) if (keys %{$self->{config}});
  27         150  
56 9         35 return $self->expandPathSimple($self->{tree}, @path);
57             }
58              
59             # override these method
60 2     2 1 23 sub expandPathSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
61 2     2 1 23 sub expandPathComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
62              
63             sub expandAll {
64 28     28 1 5398 my ($self) = @_;
65 28 100       21 return $self->expandAllComplex($self->{config}) if (keys %{$self->{config}});
  28         153  
66 7         20 return $self->expandAllSimple();
67             }
68              
69             # override these method
70 2     2 1 28 sub expandAllSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
71 2     2 1 27 sub expandAllComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
72              
73             ## private methods
74              
75             sub _compareNodeToPath {
76 117     117   116 my ($self, $current_path, $current_tree) = @_;
77             # default to normal node-path comparison ...
78             return $current_path eq $current_tree->getNodeValue()
79 117 100       263 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         21 return $self->{path_comparison_func}->($current_path, $current_tree);
83             }
84              
85             1;
86              
87             __END__