| 1 | package Tree::Simple::Visitor::CreateDirectoryTree; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 3 | my ($_class) = @_; | |||||||||||||||||||||||||||
| 15 | 3 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 16 | 3 | my $visitor = {}; | |||||||||||||||||||||||||||
| 17 | 3 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 18 | 3 | $visitor->_init(); | |||||||||||||||||||||||||||
| 19 | 3 | return $visitor; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 24 | 3 | ▶ | $self->{file_handler} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 25 | 3 | my ($filepath) = @_; | |||||||||||||||||||||||||||
| 26 | 3 | ▶ | open(FILE, ">", $filepath) || die "IO Error : Cannot create file ($filepath) : $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 3 | close(FILE); | |||||||||||||||||||||||||||
| 28 | 3 | }; | |||||||||||||||||||||||||||
| 29 | 5 | ▶ | $self->{dir_handler} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 30 | 5 | my ($dirpath) = @_; | |||||||||||||||||||||||||||
| 31 | 5 | ▶ | mkdir($dirpath) || die "IO Error : Cannot make directory ($dirpath) : $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 3 | }; | |||||||||||||||||||||||||||
| 33 | 3 | $self->SUPER::_init(); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 6 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||
| 38 | 6 | ▶ | (blessed($tree) && $tree->isa("Tree::Simple")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree)
$tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $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 | $self->_createDirectoryStructure($tree); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub setFileHandler { | ||||||||||||||||||||||||||
|
Subroutine
setFileHandler: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 4 | my ($self, $file_handler) = @_; | |||||||||||||||||||||||||||
| 46 | 4 | ▶ | (defined($file_handler) && ref($file_handler) eq 'CODE') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $file_handler and ref $file_handler eq "CODE"
MC/DC100%2/2
defined $file_handler and ref $file_handler eq "CODE"
defined $file_handler
ref $file_handler eq "CODE"
Truth table100%3/3
A and B
A= defined $file_handlerB= ref $file_handler eq "CODE"
| |||||||||||||||||||||||||||||
| 47 | || die "Insufficient Arguments : file handler must be a subroutine reference"; | ||||||||||||||||||||||||||||
| 48 | 1 | $self->{file_handler} = $file_handler; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | sub setDirectoryHandler { | ||||||||||||||||||||||||||
|
Subroutine
setDirectoryHandler: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 4 | my ($self, $dir_handler) = @_; | |||||||||||||||||||||||||||
| 53 | 4 | ▶ | (defined($dir_handler) && ref($dir_handler) eq 'CODE') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dir_handler and ref $dir_handler eq "CODE"
MC/DC100%2/2
defined $dir_handler and ref $dir_handler eq "CODE"
defined $dir_handler
ref $dir_handler eq "CODE"
Truth table100%3/3
A and B
A= defined $dir_handlerB= ref $dir_handler eq "CODE"
| |||||||||||||||||||||||||||||
| 54 | || die "Insufficient Arguments : directory handler must be a subroutine reference"; | ||||||||||||||||||||||||||||
| 55 | 1 | $self->{dir_handler} = $dir_handler; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 16 | ▶ | sub _createDirectoryStructure { | ||||||||||||||||||||||||||
|
Subroutine
_createDirectoryStructure: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 59 | 16 | my ($self, $tree, @path) = @_; | |||||||||||||||||||||||||||
| 60 | 16 | my $node = $tree->getNodeValue(); | |||||||||||||||||||||||||||
| 61 | # filter the nodes if need be | ||||||||||||||||||||||||||||
| 62 | 16 | my $filter_function = $self->getNodeFilter(); | |||||||||||||||||||||||||||
| 63 | 16 | ▶ | $node = $filter_function->($node) if $filter_function; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | # if its a leaf and it | ||||||||||||||||||||||||||||
| 65 | # doesn't end with a / | ||||||||||||||||||||||||||||
| 66 | # then its a file | ||||||||||||||||||||||||||||
| 67 | 16 | ▶ | if ($tree->isLeaf() && $node !~ /\/|\\$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tree->isLeaf and not $node =~ m[/|\\$]
MC/DC100%2/2
$tree->isLeaf and not $node =~ m[/|\\$]
$tree->isLeaf
not $node =~ m[/|\\$]
Truth table100%3/3
A and not(B)
A= $tree->isLeafB= not $node =~ m[/|\\$]
| |||||||||||||||||||||||||||||
| 68 | 6 | $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 | $node =~ s/\/|\\$//; | |||||||||||||||||||||||||||
| 74 | 10 | $self->{dir_handler}->(File::Spec->catdir(@path, $node)); | |||||||||||||||||||||||||||
| 75 | 10 | foreach my $child ($tree->getAllChildren()) { | |||||||||||||||||||||||||||
| 76 | 14 | $self->_createDirectoryStructure($child, (@path, $node)); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||