Navigator/Node/Filesys.pm

filter: Statementstmt 36.3% 28 / 77 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 44.4% 8 / 18 Podpod 50.0% 2 / 4 totaltotal 30.6% 38 / 124 CC 13 SCAR 22.0
CC
13
Cov
28%
_children
30.0
_content
12.0
MOUNT
6.0
CRAP 9.0
« Navigator.pm ↑ Summary Navigator/Node.pm »
1 package Tree::Navigator::Node::Filesys;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Tree::Navigator::Node';
5  
6 1 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::MoreUtils qw/part/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Params::Validate qw/validate SCALAR SCALARREF/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub MOUNT {
Subroutine MOUNT: not called CC=2 SCAR=17.9
Pod documented
14 0 my ($class, $mount_args) = @_;
15 0 ▶ my @mount_point = %{$mount_args->{mount_point} || {}};
Condition0%0/2
$mount_args->{'mount_point'} || {}
l!l
00
MC/DC0%0/1
$mount_args->{'mount_point'} || {}
$mount_args->{'mount_point'}
Truth table0%0/2
A
A= $mount_args->{'mount_point'}
Aresult
11
00
16 0 $mount_args->{mount_point} = validate(@mount_point , {
17 root => {type => SCALAR},
18 exclude => {type => SCALARREF, isa => 'Regexp', default => qr/^\./},
19 });
20 }
21  
22  
23 0 ▶sub file_path {
Subroutine file_path: not called CC=1 SCAR=6.9
Pod undocumented
24 0 my $self = shift;
25 0 return $self->_join_path($self->mount_point->{root}, $self->path);
26 }
27  
28  
29 0 ▶sub is_parent {
Subroutine is_parent: not called CC=1 SCAR=6.9
Pod documented
30 0 my $self = shift;
31 0 return -d $self->file_path;
32 }
33  
34  
35 0 ▶sub _children {
Subroutine _children: not called CC=5 SCAR=34.0
36 0 my $self = shift;
37 0 my $file_path = $self->file_path;
38 0 ▶ if (-d $file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file_path) { } 0 0 0
39 # read and filter entries from the directory
40 0 ▶ opendir my $dh, $file_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $file_path or die $! 0 0 0
41 0 my $regex = $self->mount_point->{exclude};
42 0 my @entries = grep {$_ !~ $regex} readdir $dh;
43  
44 # case-insensitive sort, first for dirs, then for files
45 0 ▶ my ($dirs, $files) = part {-d "$file_path/$_" ? 0 : 1} @entries;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-d "$file_path/$_" ? : 0 0 0
46 0 ▶ $_ ||= [] for $dirs, $files;
Condition0%0/2
$_ ||= []
l!l
00
MC/DC0%0/1
$_ ||= []
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
47 0 return [ (sort {lc($a) cmp lc($b)} @$dirs),
48 0 (sort {lc($a) cmp lc($b)} @$files) ];
49 }
50 else {
51 # non-directories have no children
52 0 return [];
53 }
54 }
55  
56  
57 0 ▶sub _child {
Subroutine _child: not called CC=2 SCAR=17.9
58 0 my ($self, $child_path) = @_;
59 0 my $class = ref $self;
60 0 my $file_path = $self->file_path;
61 0 ▶ -e "$file_path/$child_path" or die "$file_path has no child '$child_path'";
Branch: 1 branch
BranchTrueFalseTotal
-e "$file_path/$child_path" or die "$file_path has no child '${child_path}'" 0 0 0
62 0 return $class->new(
63 mount_point => $self->mount_point,
64 path => $self->_join_path($self->path, $child_path),
65 );
66 }
67  
68  
69  
70  
71  
72 0 ▶sub _attributes {
Subroutine _attributes: not called CC=2 SCAR=17.9
73 0 my $self = shift;
74 0 my $file = $self->file_path;
75 0 my @stats = stat $file;
76 0 my %attrs;
77 0 $attrs{modified} = HTTP::Date::time2str($stats[9]);
78 0 ▶ if (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 0 0
79 0 $attrs{size} = $stats[7];
80 }
81 0 return \%attrs;
82 }
83  
84 0 ▶sub _content {
Subroutine _content: not called CC=3 SCAR=24.8
85 0 my $self = shift;
86 0 my $file = $self->file_path;
87 0 ▶ return undef if ! -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
88  
89 0 ▶ open my $fh, "<:raw", $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die $! 0 0 0
90 0 return $fh;
91 }
92  
93  
94  
95  
96 0 ▶override 'response' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
97 0 my $self = shift;
98 0 my $file = $self->file_path;
99  
100 0 ▶ return -d $file ? super() : $self->file_response;
Branch: 1 branch
BranchTrueFalseTotal
-d $file ? : 0 0 0
101  
102 1 };
103  
104  
105 # code mostly borrowed from Plack::App::File
106 0 ▶sub file_response {
Subroutine file_response: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $self = shift;
108 0 my $fh = $self->_content;
109 0 my $file = $self->file_path;
110 0 ▶ my $content_type = Plack::MIME->mime_type($file) || 'text/plain';
Condition0%0/2
'Plack::MIME'->mime_type($file) || 'text/plain'
l!l
00
MC/DC0%0/1
'Plack::MIME'->mime_type($file) || 'text/plain'
'Plack::MIME'->mime_type($file)
Truth table0%0/2
A
A= 'Plack::MIME'->mime_type($file)
Aresult
11
00
111 0 Plack::Util::set_io_path($fh, Cwd::realpath($file));
112  
113 # TODO : SUPPORT CONDITIONAL GET
114  
115 return [
116 200,
117 [
118 'Content-Type' => $content_type,
119 'Content-Length' => $self->attributes->{size},
120 'Last-Modified' => $self->attributes->{modified},
121 0 ],
122 $fh,
123 ];
124 }
125  
126  
127 1 __PACKAGE__->meta->make_immutable;
128  
129  
130 1 1; # End of Tree::Navigator::Node::Filesys
131  
132 __END__
« Navigator.pm ↑ Summary Navigator/Node.pm »