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   16799 use strict;
  1         2  
  1         23  
4 1     1   4 use warnings;
  1         1  
  1         30  
5              
6             our $VERSION = '0.14';
7              
8 1     1   3 use File::Spec;
  1         2  
  1         16  
9 1     1   3 use Scalar::Util qw(blessed);
  1         1  
  1         89  
10              
11 1     1   4 use base qw(Tree::Simple::Visitor);
  1         1  
  1         471  
12              
13             sub new {
14 3     3 1 12608 my ($_class) = @_;
15 3   33     17 my $class = ref($_class) || $_class;
16 3         4 my $visitor = {};
17 3         4 bless($visitor, $class);
18 3         6 $visitor->_init();
19 3         19 return $visitor;
20             }
21              
22             sub _init {
23 3     3   3 my ($self) = @_;
24             $self->{file_handler} = sub {
25 3     3   3 my ($filepath) = @_;
26 3 50       205 open(FILE, ">", $filepath) || die "IO Error : Cannot create file ($filepath) : $!";
27 3         26 close(FILE);
28 3         16 };
29             $self->{dir_handler} = sub {
30 5     5   6 my ($dirpath) = @_;
31 5 50       276 mkdir($dirpath) || die "IO Error : Cannot make directory ($dirpath) : $!";
32 3         9 };
33 3         11 $self->SUPER::_init();
34             }
35              
36             sub visit {
37 6     6 1 2535 my ($self, $tree) = @_;
38 6 100 100     64 (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         6 $self->_createDirectoryStructure($tree);
42             }
43              
44             sub setFileHandler {
45 4     4 1 993 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         3 $self->{file_handler} = $file_handler;
49             }
50              
51             sub setDirectoryHandler {
52 4     4 1 2544 my ($self, $dir_handler) = @_;
53 4 100 100     39 (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   24 my ($self, $tree, @path) = @_;
60 16         27 my $node = $tree->getNodeValue();
61             # filter the nodes if need be
62 16         52 my $filter_function = $self->getNodeFilter();
63 16 100       55 $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     39 if ($tree->isLeaf() && $node !~ /\/|\\$/) {
68 6         78 $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         96 $node =~ s/\/|\\$//;
74 10         51 $self->{dir_handler}->(File::Spec->catdir(@path, $node));
75 10         37 foreach my $child ($tree->getAllChildren()) {
76 14         77 $self->_createDirectoryStructure($child, (@path, $node));
77             }
78             }
79             }
80              
81             1;
82              
83             __END__