EO/File.pm

filter: Statementstmt 55.4% 61 / 110 Branchbran 28.5% 8 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 65.2% 15 / 23 Podpod 7.1% 1 / 14 totaltotal 48.5% 85 / 175 CC 15 SCAR 13.4
CC
15
Cov
50%
handle
12.0
isfile
6.0
readable
6.0
CRAP 3.8
« EO/Array.pm ↑ Summary EO/WeakArray.pm »
1 package EO::File;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use EO::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use EO::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use EO::delegate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = 0.96;
14 2 our @ISA = qw(EO::Storage);
15  
16 2 exception EO::Error::File;
17 2 exception EO::Error::File::NotFound
18 extends => 'EO::Error::File';
19 2 exception EO::Error::File::Permission
20 extends => 'EO::Error::File';
21 2 exception EO::Error::File::IsDirectory
22 extends => 'EO::Error::File';
23 2 exception EO::Error::File::Permission::Read
24 extends => 'EO::Error::File::Permission';
25 2 exception EO::Error::File::Open
26 extends => 'Eo::Error::File';
27  
28 3 ▶sub init {
Subroutine init: called CC=3 SCAR=11.2
Pod undocumented
29 3 my $self = shift;
30  
31 3 ▶ return 0 unless ($self->NEXT::init(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->NEXT::init(@_) 0 3 3
32  
33 3 my %params = @_;
34  
35 3 ▶ if ( my $file = $params{path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $params{"path"}) 3 0 3
36 3 $self->path($file)
37 }
38  
39 3 return 1;
40 }
41  
42 3 ▶sub path {
Subroutine path: called CC=4 SCAR=15.0
Pod undocumented
43 3 my $self = shift;
44 3 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
45 3 my $path = shift;
46 3 ▶ unless(UNIVERSAL::isa($path, 'Path::Class::File')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($path, "Path::Class::File")) 3 0 3
47 $path = Path::Class::File->new(ref $path
48 0 ▶ ? @{$path}
Branch: 1 branch
BranchTrueFalseTotal
ref $path ? : 0 3 3
49 : $path
50 );
51 3 $path = $path->absolute();
52 }
53 3 $self->delegate($path);
54 3 return $self;
55 }
56 0 return $self->delegate();
57 }
58  
59 2 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
60 2 my $self = shift;
61 2 return $self->stringify();
62 }
63  
64 1 ▶sub exists {
Subroutine exists: called CC=2 SCAR=7.0
Pod undocumented
65 1 my $self = shift;
66 1 my $filename = $self->as_string;
67 1 ▶ unless(-e $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 1 0 1
68 1 throw EO::Error::File::NotFound
69 text => "Cannot open file: $filename, file not found",
70 filename => $filename;
71 }
72 }
73  
74 0 ▶sub isfile {
Subroutine isfile: not called CC=2 SCAR=17.9
Pod undocumented
75 0 my $self = shift;
76 0 my $filename = $self->as_string;
77 0 ▶ if (-d $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $filename) 0 0 0
78 0 throw EO::Error::File::IsDirectory
79 text => "Cannot open file: $filename, path is a directory";
80 }
81 }
82  
83 0 ▶sub readable {
Subroutine readable: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my $self = shift;
85 0 my $filename = $self->as_string;
86 0 ▶ unless(-r $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $filename) 0 0 0
87 0 throw EO::Error::File::Permission::Read
88 text => "Cannot open file: $filename, file not readable";
89 }
90 }
91  
92 0 ▶sub handle {
Subroutine handle: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my $self = shift;
94 0 my $mode = shift;
95 0 ▶ if (!$mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode) 0 0 0
96 0 throw EO::Error::InvalidParameters
97 text => "no mode supplied";
98 }
99 0 my $fh = IO::File->new( $self->as_string, $mode );
100 0 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
101 0 my $err = 'Cannot open file: ';
102 0 $err .= $self->as_string;
103 0 $err .= " with mode $mode ($!)";
104 0 throw EO::Error::File::Open text => $err;
105 }
106 0 return $fh;
107 }
108  
109 1 ▶sub load {
Subroutine load: called CC=1 SCAR=4.1
Pod undocumented
110 1 my $self = shift;
111  
112 1 $self->exists;
113 0 $self->isfile;
114 0 $self->readable;
115  
116 0 my $fh = $self->handle("<");
117  
118 0 local($/);
119 0 my $raw_content = $fh->getline;
120  
121 0 $self->data(
122 EO::Data->new()
123 ->storage( $self )
124 ->content( \$raw_content )
125 );
126  
127 0 $fh->close();
128  
129 0 return $self->data;
130 }
131  
132 3 ▶sub data {
Subroutine data: called CC=2 SCAR=8.4
Pod undocumented
133 3 my $self = shift;
134 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
135 0 $self->{ e_file_data } = shift;
136 0 return $self;
137 }
138 3 return $self->{ e_file_data };
139 }
140  
141 0 ▶sub file_error {
Subroutine file_error: not called CC=1 SCAR=6.9
Pod undocumented
142 0 my $self = shift;
143 0 throw EO::Error::File
144 text => "Could not perform operation on file ", $self->as_string, " ($!)";
145 }
146  
147 0 ▶sub unlink {
Subroutine unlink: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my $self = shift;
149 0 ▶ unlink($self->as_string) || $self->file_error;
Branch: 1 branch
BranchTrueFalseTotal
CORE::unlink $self->as_string or $self->file_error 0 0 0
150 0 return $self;
151 }
152  
153  
154 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
155 0 my $self = shift;
156 0 my $data = shift;
157  
158 0 my $filename = $self->as_string();
159 0 my $parent = $self->dir();
160  
161 0 my $fh = $self->handle( "+>" );
162 0 my $content = $data->content_ref;
163  
164 0 ▶ $fh->print( $$content ) || $self->file_error;
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($$content) or $self->file_error 0 0 0
165 0 $fh->close();
166 }
167  
168 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.6
169 3 my $self = shift;
170  
171 3 ▶ if ($self->data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data) 0 3 3
172 0 $self->data->delete_storage( $self );
173 }
174  
175 3 $self->NEXT::DESTROY;
176 }
177  
178 0 ▶sub make_absolute {
Subroutine make_absolute: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my $self = shift;
180 0 $self->delegate($self->absolute);
181 }
182  
183 0 ▶sub make_relative {
Subroutine make_relative: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my $self = shift;
185 0 $self->delegate($self->absolute);
186 }
187  
188 2 1;
« EO/Array.pm ↑ Summary EO/WeakArray.pm »