blib/lib/File/Object.pm

filter: Statementstmt 100% 134 / 134 Branchbran 100% 42 / 42 Conditioncond 100% 17 / 17 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 18 / 18 Podpod 100% 9 / 9 totaltotal 99.5% 228 / 229 SCAR 34.0
CC
30
Cov
100%
new
10.0
up
7.0
_update_path
5.0
CRAP 30.0
↑ Summary
1 package File::Object;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use File::Spec::Functions qw(catdir catfile splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use FindBin qw($Bin $Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = 0.16;
12  
13 # Constructor.
14 sub new {
15 39 ▶ my ($class, @params) = @_;
Subroutine new: called CC=10 SCAR=23.0
Pod documented
16  
17 # Create object.
18 39 my $self = bless {}, $class;
19  
20 # Directory path.
21 39 $self->{'dir'} = [];
22  
23 # File path.
24 39 $self->{'file'} = undef;
25  
26 # Type of path.
27 39 $self->{'type'} = 'dir';
28  
29 # Process parameters.
30 39 set_params($self, @params);
31  
32 # Check to right 'type'.
33 37 ▶ if (! $self->{'type'} || ($self->{'type'} ne 'file'
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"type"} or $self->{"type"} ne "file" and $self->{"type"} ne "dir") 2 35 37
Condition100%6/6
not $self->{"type"} or $self->{"type"} ne "file" and $self->{"type"} ne "dir"
l!l&&r!l&&!r
1135
$self->{"type"} ne "file" and $self->{"type"} ne "dir"
!ll&&!rl&&r
15201
MC/DC66%2/3
not $self->{"type"} or $self->{"type"} ne "file" and $self->{"type"} ne "dir"
not $self->{"type"} $self->{"type"} ne "file" $self->{"type"} ne "dir"
Truth table75%3/4
not(A) or B and C
A= not $self->{"type"}B= $self->{"type"} ne "file"C= $self->{"type"} ne "dir"
ABCresult
1XX1
0111
00X0
0100
34 && $self->{'type'} ne 'dir')) {
35  
36 2 err 'Bad \'type\' parameter.';
37 }
38  
39 # Check to dir.
40 35 ▶ if (ref $self->{'dir'} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"dir"} ne "ARRAY") 1 34 35
41 1 err '\'dir\' parameter must be a reference to array.';
42 }
43  
44 # Check to file.
45 34 ▶ if ($self->{'type'} eq 'file' && @{$self->{'dir'}}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "file" and @{$self->{"dir"};} and not defined $self->{"file"}) 1 33 34
Condition100%6/6
$self->{"type"} eq "file" and @{$self->{"dir"};} and not defined $self->{"file"}
!ll&&!rl&&r
22111
$self->{"type"} eq "file" and @{$self->{"dir"};}
!ll&&!rl&&r
19312
MC/DC100%3/3
$self->{"type"} eq "file" and @{$self->{"dir"};} and not defined $self->{"file"}
$self->{"type"} eq "file" @{$self->{"dir"};} not defined $self->{"file"}
Truth table100%4/4
A and B and not(C)
A= $self->{"type"} eq "file"B= @{$self->{"dir"};}C= not defined $self->{"file"}
ABCresult
0XX0
10X0
1100
1111
46 && ! defined $self->{'file'}) {
47  
48 1 err 'Bad file constructor with undefined \'file\' parameter.';
49 }
50  
51 # Remove undef dirs.
52 33 ▶ if (@{$self->{'dir'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"dir"};}) 20 13 33
53 20 ▶ my @dir = map { defined $_ ? $_ : () } @{$self->{'dir'}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 33 1 34
54 20 $self->{'dir'} = \@dir;
55 }
56  
57 # Update path.
58 33 $self->_update_path;
59  
60 # Set values.
61 33 $self->set;
62  
63 # Object.
64 33 return $self;
65 }
66  
67 # Add dir.
68 sub dir {
69 17 ▶ my ($self, @dirs) = @_;
Subroutine dir: called CC=3 SCAR=11.0
Pod documented
70  
71 17 foreach my $dir (@dirs) {
72 8 ▶ if (defined $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir) 6 2 8
73 6 $self->_dir($dir);
74 }
75 }
76  
77 # Object.
78 17 return $self;
79 }
80  
81 # Add file.
82 sub file {
83 12 ▶ my ($self, @file_path) = @_;
Subroutine file: called CC=1 SCAR=0.0
Pod documented
84  
85 12 my $file = pop @file_path;
86 12 $self->dir(@file_path);
87 12 $self->_file($file);
88  
89 12 return $self;
90 }
91  
92  
93 # Get dir.
94 sub get_dir {
95 12 ▶ my ($self, $dir_num) = @_;
Subroutine get_dir: called CC=3 SCAR=11.0
Pod documented
96  
97 12 ▶ $dir_num ||= 1;
Condition100%2/2
$dir_num ||= 1
l!l
210
MC/DC100%1/1
$dir_num ||= 1
$dir_num
Truth table100%2/2
A
A= $dir_num
Aresult
11
00
98 12 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "file") 3 9 12
99 3 $dir_num++;
100 }
101  
102 12 return $self->{'path'}->[-$dir_num];
103 }
104  
105 # Get file.
106 sub get_file {
107 2 ▶ my $self = shift;
Subroutine get_file: called CC=2 SCAR=6.9
Pod documented
108  
109 2 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'file') { } 1 1 2
110 1 return $self->{'path'}->[-1];
111 } else {
112 1 return;
113 }
114 }
115  
116 # Reset to constructor values.
117 sub reset {
118 10 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
119  
120 # Reset to setted values.
121 10 $self->{'dir'} = $self->{'_set'}->{'dir'};
122 10 $self->{'file'} = $self->{'_set'}->{'file'};
123 10 $self->{'type'} = $self->{'_set'}->{'type'};
124  
125 # Update path.
126 10 $self->_update_path;
127  
128 10 return $self;
129 }
130  
131 # Serialize path.
132 sub s {
133 34 ▶ my $self = shift;
Subroutine s: called CC=2 SCAR=6.9
Pod documented
134  
135 34 ▶ if ($self->{'type'} eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'dir') { } 15 19 34
136 15 return catdir(@{$self->{'path'}});
137 } else {
138 19 return catfile(@{$self->{'path'}});
139 }
140 }
141  
142 # Set actual values to constructor values.
143 sub set {
144 35 ▶ my $self = shift;
Subroutine set: called CC=2 SCAR=6.9
Pod documented
145  
146 35 my @path = @{$self->{'path'}};
147 35 $self->{'_set'}->{'type'} = $self->{'type'};
148 35 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'file') { } 15 20 35
149 15 $self->{'_set'}->{'file'} = pop @path;
150 15 $self->{'_set'}->{'dir'} = \@path;
151 } else {
152 20 $self->{'_set'}->{'dir'} = \@path;
153 }
154  
155 35 return $self;
156 }
157  
158 # Go to parent directory.
159 sub up {
160 9 ▶ my ($self, $up_num) = @_;
Subroutine up: called CC=7 SCAR=19.5
Pod documented
161  
162 # Check number and positive number.
163 9 ▶ if (! $up_num || $up_num !~ m/^\d$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($up_num and $up_num =~ /^\d$/ms) 7 2 9
Condition100%3/3
$up_num and $up_num =~ /^\d$/ms
!ll&&!rl&&r
522
MC/DC100%2/2
$up_num and $up_num =~ /^\d$/ms
$up_num $up_num =~ /^\d$/ms
Truth table100%3/3
A and B
A= $up_numB= $up_num =~ /^\d$/ms
ABresult
0X0
100
111
164 7 $up_num = 1;
165 }
166  
167 # Process.
168 9 foreach (1 .. $up_num) {
169 10 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'file') { } 2 8 10
170 2 ▶ if (@{$self->{'path'}} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'path'};} > 2) { } 1 1 2
171 1 $self->{'type'} = 'dir';
172 1 $self->{'file'} = undef;
173 1 splice @{$self->{'path'}}, -2;
174 } else {
175 1 err 'Cannot go up.', 'PATH', $self->s;
176 }
177 } else {
178 8 ▶ if (@{$self->{'path'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'path'};}) { } 7 1 8
179 7 pop @{$self->{'path'}};
180 } else {
181 1 err 'Cannot go up.', 'PATH', $self->s;
182 }
183 }
184 }
185  
186 # Object.
187 7 return $self;
188 }
189  
190 # Add dir array.
191 sub _dir {
192 6 ▶ my ($self, @dir) = @_;
Subroutine _dir: called CC=2 SCAR=6.9
193  
194 6 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "file") 1 5 6
195 1 $self->{'type'} = 'dir';
196 1 $self->{'file'} = undef;
197 1 pop @{$self->{'path'}};
198 }
199 6 push @{$self->{'path'}}, @dir;
200  
201 6 return;
202 }
203  
204 # Add file array.
205 sub _file {
206 12 ▶ my ($self, $file) = @_;
Subroutine _file: called CC=3 SCAR=11.0
207  
208 12 ▶ if (! defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 11 12
209 1 return;
210 }
211 11 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'file') { } 6 5 11
212 6 pop @{$self->{'path'}};
213 6 push @{$self->{'path'}}, $file;
214 } else {
215 5 push @{$self->{'path'}}, $file;
216 5 $self->{'type'} = 'file';
217 }
218  
219 11 return;
220 }
221  
222 # Update path.
223 sub _update_path {
224 43 ▶ my $self = shift;
Subroutine _update_path: called CC=5 SCAR=16.1
225  
226 43 ▶ if ($self->{'type'} eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} eq 'file') { } 17 26 43
227 $self->{'path'} = [
228 17 ▶ @{$self->{'dir'}},
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 15 2 17
229 17 defined $self->{'file'} ? $self->{'file'} : (),
230 ];
231 17 ▶ if (! @{$self->{'path'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"path"};}) 2 15 17
232 2 $self->{'path'} = [splitdir($Bin), $Script];
233 }
234 } else {
235 26 $self->{'path'} = [@{$self->{'dir'}}];
236 26 ▶ if (! @{$self->{'path'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"path"};}) 10 16 26
237 10 $self->{'path'} = [splitdir($Bin)];
238 }
239 }
240  
241 43 return;
242 }
243  
244 11 1;
245  
246 __END__
↑ Summary