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 6     6   17106 use strict;
  6         9  
  6         142  
4 6     6   18 use warnings;
  6         18  
  6         213  
5              
6             our $VERSION = '0.180001';
7              
8 6     6   21 use Scalar::Util qw(blessed);
  6         12  
  6         482  
9              
10 6     6   1323 use Tree::Simple::View::Exceptions;
  6         7  
  6         2467  
11              
12             sub new {
13 56     56 1 41773 my ($_class, $tree, %configuration) = @_;
14 56   33     220 my $class = ref($_class) || $_class;
15 56 100       106 ($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 55         211 my $tree_view = bless{
18             tree => undef,
19             config => {},
20             include_trunk => undef,
21             ppath_comparison_func => undef
22             } => $class;
23 55         429 $tree_view->_init($tree, %configuration);
24 51         104 return $tree_view;
25             }
26              
27             sub _init {
28 55     55   80 my ($self, $tree, %config) = @_;
29 55 100 100     399 (blessed($tree) && $tree->isa("Tree::Simple"))
30             || throw Tree::Simple::View::InsufficientArguments "tree argument must be a Tree::Simple object";
31 51         78 $self->{tree} = $tree;
32 51 100       101 $self->{config} = \%config if %config;
33 51         50 $self->{include_trunk} = 0;
34 51         75 $self->{path_comparison_func} = undef;
35             }
36              
37 1     1 1 904 sub getTree { (shift)->{tree} }
38 1     1 1 5 sub getConfig { (shift)->{config} }
39              
40             sub includeTrunk {
41 14     14 1 4222 my ($self, $boolean) = @_;
42 14 50       55 $self->{include_trunk} = ($boolean ? 1 : 0) if defined $boolean;
    50          
43 14         19 return $self->{include_trunk};
44             }
45              
46             sub setPathComparisonFunction {
47 4     4 1 927 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 4940 my ($self, @path) = @_;
55 27 100       26 return $self->expandPathComplex($self->{tree}, $self->{config}, @path) if (keys %{$self->{config}});
  27         138  
56 9         32 return $self->expandPathSimple($self->{tree}, @path);
57             }
58              
59             # override these method
60 2     2 1 21 sub expandPathSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
61 2     2 1 21 sub expandPathComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
62              
63             sub expandAll {
64 26     26 1 5331 my ($self) = @_;
65 26 100       26 return $self->expandAllComplex($self->{config}) if (keys %{$self->{config}});
  26         144  
66 7         22 return $self->expandAllSimple();
67             }
68              
69             # override these method
70 2     2 1 30 sub expandAllSimple { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
71 2     2 1 26 sub expandAllComplex { throw Tree::Simple::View::AbstractMethod "Method Not Implemented" }
72              
73             ## private methods
74              
75             sub _compareNodeToPath {
76 117     117   100 my ($self, $current_path, $current_tree) = @_;
77             # default to normal node-path comparison ...
78             return $current_path eq $current_tree->getNodeValue()
79 117 100       243 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         17 return $self->{path_comparison_func}->($current_path, $current_tree);
83             }
84              
85             1;
86              
87             __END__