File Coverage

blib/lib/Tree/Simple/Visitor/CreateDirectoryTree.pm
Criterion Covered Total %
statement 49 49 100.0
branch 12 14 85.7
condition 13 15 86.6
subroutine 13 13 100.0
pod 4 4 100.0
total 91 95 95.7


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::CreateDirectoryTree;
2              
3 1     1   59893 use strict;
  1         9  
  1         23  
4 1     1   5 use warnings;
  1         1  
  1         30  
5              
6             our $VERSION = '0.16';
7              
8 1     1   4 use File::Spec;
  1         1  
  1         30  
9 1     1   5 use Scalar::Util qw(blessed);
  1         1  
  1         53  
10              
11 1     1   6 use base qw(Tree::Simple::Visitor);
  1         1  
  1         464  
12              
13             sub new {
14 3     3 1 12273 my ($_class) = @_;
15 3   33     16 my $class = ref($_class) || $_class;
16 3         5 my $visitor = {};
17 3         7 bless($visitor, $class);
18 3         9 $visitor->_init();
19 3         24 return $visitor;
20             }
21              
22             sub _init {
23 3     3   6 my ($self) = @_;
24             $self->{file_handler} = sub {
25 3     3   7 my ($filepath) = @_;
26 3 50       151 open(FILE, ">", $filepath) || die "IO Error : Cannot create file ($filepath) : $!";
27 3         38 close(FILE);
28 3         17 };
29             $self->{dir_handler} = sub {
30 5     5   27 my ($dirpath) = @_;
31 5 50       292 mkdir($dirpath) || die "IO Error : Cannot make directory ($dirpath) : $!";
32 3         11 };
33 3         13 $self->SUPER::_init();
34             }
35              
36             sub visit {
37 6     6 1 2425 my ($self, $tree) = @_;
38 6 100 100     69 (blessed($tree) && $tree->isa("Tree::Simple"))
39             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
40             # pass on to our recursive subroutine
41 2         7 $self->_createDirectoryStructure($tree);
42             }
43              
44             sub setFileHandler {
45 4     4 1 888 my ($self, $file_handler) = @_;
46 4 100 100     42 (defined($file_handler) && ref($file_handler) eq 'CODE')
47             || die "Insufficient Arguments : file handler must be a subroutine reference";
48 1         6 $self->{file_handler} = $file_handler;
49             }
50              
51             sub setDirectoryHandler {
52 4     4 1 2298 my ($self, $dir_handler) = @_;
53 4 100 100     36 (defined($dir_handler) && ref($dir_handler) eq 'CODE')
54             || die "Insufficient Arguments : directory handler must be a subroutine reference";
55 1         6 $self->{dir_handler} = $dir_handler;
56             }
57              
58             sub _createDirectoryStructure {
59 16     16   32 my ($self, $tree, @path) = @_;
60 16         35 my $node = $tree->getNodeValue();
61             # filter the nodes if need be
62 16         60 my $filter_function = $self->getNodeFilter();
63 16 100       82 $node = $filter_function->($node) if $filter_function;
64             # if its a leaf and it
65             # doesn't end with a /
66             # then its a file
67 16 100 100     53 if ($tree->isLeaf() && $node !~ /\/|\\$/) {
68 6         98 $self->{file_handler}->(File::Spec->catfile(@path, $node));
69             }
70             # otherwise we are going
71             # to treat it as a directory
72             else {
73 10         127 $node =~ s/\/|\\$//;
74 10         66 $self->{dir_handler}->(File::Spec->catdir(@path, $node));
75 10         51 foreach my $child ($tree->getAllChildren()) {
76 14         89 $self->_createDirectoryStructure($child, (@path, $node));
77             }
78             }
79             }
80              
81             1;
82              
83             __END__