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   16578 use strict;
  1         2  
  1         22  
4 1     1   3 use warnings;
  1         1  
  1         27  
5              
6             our $VERSION = '0.15';
7              
8 1     1   4 use File::Spec;
  1         1  
  1         14  
9 1     1   3 use Scalar::Util qw(blessed);
  1         1  
  1         113  
10              
11 1     1   4 use base qw(Tree::Simple::Visitor);
  1         1  
  1         452  
12              
13             sub new {
14 3     3 1 12218 my ($_class) = @_;
15 3   33     14 my $class = ref($_class) || $_class;
16 3         4 my $visitor = {};
17 3         4 bless($visitor, $class);
18 3         6 $visitor->_init();
19 3         15 return $visitor;
20             }
21              
22             sub _init {
23 3     3   3 my ($self) = @_;
24             $self->{file_handler} = sub {
25 3     3   2 my ($filepath) = @_;
26 3 50       141 open(FILE, ">", $filepath) || die "IO Error : Cannot create file ($filepath) : $!";
27 3         25 close(FILE);
28 3         13 };
29             $self->{dir_handler} = sub {
30 5     5   32 my ($dirpath) = @_;
31 5 50       235 mkdir($dirpath) || die "IO Error : Cannot make directory ($dirpath) : $!";
32 3         8 };
33 3         10 $self->SUPER::_init();
34             }
35              
36             sub visit {
37 6     6 1 2516 my ($self, $tree) = @_;
38 6 100 100     61 (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         5 $self->_createDirectoryStructure($tree);
42             }
43              
44             sub setFileHandler {
45 4     4 1 1004 my ($self, $file_handler) = @_;
46 4 100 100     41 (defined($file_handler) && ref($file_handler) eq 'CODE')
47             || die "Insufficient Arguments : file handler must be a subroutine reference";
48 1         2 $self->{file_handler} = $file_handler;
49             }
50              
51             sub setDirectoryHandler {
52 4     4 1 2433 my ($self, $dir_handler) = @_;
53 4 100 100     37 (defined($dir_handler) && ref($dir_handler) eq 'CODE')
54             || die "Insufficient Arguments : directory handler must be a subroutine reference";
55 1         3 $self->{dir_handler} = $dir_handler;
56             }
57              
58             sub _createDirectoryStructure {
59 16     16   23 my ($self, $tree, @path) = @_;
60 16         22 my $node = $tree->getNodeValue();
61             # filter the nodes if need be
62 16         53 my $filter_function = $self->getNodeFilter();
63 16 100       50 $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     34 if ($tree->isLeaf() && $node !~ /\/|\\$/) {
68 6         77 $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         117 $node =~ s/\/|\\$//;
74 10         54 $self->{dir_handler}->(File::Spec->catdir(@path, $node));
75 10         32 foreach my $child ($tree->getAllChildren()) {
76 14         76 $self->_createDirectoryStructure($child, (@path, $node));
77             }
78             }
79             }
80              
81             1;
82              
83             __END__