lib/Badger/Filesystem/File.pm

filter: Statementstmt 93.5% 58 / 62 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 100% 19 / 19 totaltotal 89.4% 102 / 114 CC 7 SCAR 8.1
CC
7
Cov
86%
file
6.0
data
5.1
directory
2.1
CRAP 2.2
« lib/Badger/Class/Config.pm ↑ Summary lib/Badger/Codec/Unicode.pm »
1 #========================================================================
2 #
3 # Badger::Filesystem::File
4 #
5 # DESCRIPTION
6 # OO representation of a file in a filesystem.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Filesystem::File;
14  
15 use Badger::Class
16 70 version => 0.01,
17 base => 'Badger::Filesystem::Path',
18 debug => 0,
19 dumps => 'path volume directory name stats',
20 import => 'class',
21 constants => 'ARRAY BLANK',
22 constant => {
23 type => 'File',
24 is_file => 1,
25 70 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 # aliases
29 70 *base = \&directory;
30 70 *copy = \&copy_to;
31 70 *move = \&move_to;
32  
33  
34 sub init {
35 290 ▶ my ($self, $config) = @_;
Subroutine init: called CC=1 SCAR=0.0
Pod documented
36 290 $self->init_path($config);
37 290 $self->init_options($config);
38 290 return $self;
39 }
40  
41 sub directory {
42 5 ▶ my $self = shift;
Subroutine directory: called CC=2 SCAR=7.2
Pod documented
43 return @_
44 5 ▶ ? $self->filesystem->directory( $self->relative(@_) )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
45 : $self->parent;
46 }
47  
48 sub file {
49 0 ▶ my $self = shift;
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
50 return @_
51 0 ▶ ? $self->filesystem->file( $self->relative(@_) )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
52 : $self;
53 }
54  
55 sub exists {
56 71 ▶ my $self = shift;
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
57 # cache the stats returned in case we want them later
58 71 return ($self->{ stats } = $self->filesystem->file_exists($self->{ path }));
59 }
60  
61 sub create {
62 2 ▶ my $self = shift;
Subroutine create: called CC=1 SCAR=0.0
Pod documented
63 2 $self->filesystem->create_file($self->{ path }, @_);
64 }
65  
66 sub touch {
67 2 ▶ my $self = shift;
Subroutine touch: called CC=1 SCAR=0.0
Pod documented
68 2 $self->filesystem->touch_file($self->{ path });
69 }
70  
71 sub open {
72 3 ▶ my $self = shift;
Subroutine open: called CC=1 SCAR=0.0
Pod documented
73 3 $self->filesystem->open_file($self->{ path }, @_, $self->{ options });
74 }
75  
76 sub read {
77 27 ▶ my $self = shift;
Subroutine read: called CC=1 SCAR=0.0
Pod documented
78 27 $self->filesystem->read_file($self->{ path }, @_, $self->{ options });
79 }
80  
81 sub write {
82 12 ▶ my $self = shift;
Subroutine write: called CC=1 SCAR=0.0
Pod documented
83 12 $self->filesystem->write_file($self->{ path }, @_, $self->{ options });
84 }
85  
86 sub append {
87 2 ▶ my $self = shift;
Subroutine append: called CC=1 SCAR=0.0
Pod documented
88 2 $self->filesystem->append_file($self->{ path }, @_, $self->{ options });
89 }
90  
91 sub copy_to {
92 2 ▶ my $self = shift;
Subroutine copy_to: called CC=1 SCAR=0.0
Pod documented
93 2 $self->filesystem->copy_file($self->{ path }, @_);
94 }
95  
96 sub copy_from {
97 1 ▶ my $self = shift;
Subroutine copy_from: called CC=1 SCAR=0.0
Pod documented
98 1 $self->filesystem->copy_file(shift, $self->{ path }, @_);
99 }
100  
101 sub move_to {
102 1 ▶ my $self = shift;
Subroutine move_to: called CC=1 SCAR=0.0
Pod documented
103 1 $self->filesystem->move_file($self->{ path }, @_);
104 }
105  
106 sub move_from {
107 0 ▶ my $self = shift;
Subroutine move_from: not called CC=1 SCAR=6.9
Pod documented
108 0 $self->filesystem->move_file(shift, $self->{ path }, @_);
109 }
110  
111 sub print {
112 2 ▶ my $self = shift;
Subroutine print: called CC=1 SCAR=0.0
Pod documented
113 2 $self->write( join(BLANK, @_) );
114 }
115  
116 sub delete {
117 11 ▶ my $self = shift;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
118 11 $self->filesystem->delete_file($self->{ path }, @_);
119 }
120  
121 sub text {
122 18 ▶ my $self = shift;
Subroutine text: called CC=1 SCAR=0.0
Pod documented
123 18 my $text = $self->read(@_, $self->{ options });
124 18 return $text;
125 }
126  
127 sub data {
128 11 ▶ my $self = shift;
Subroutine data: called CC=5 SCAR=16.3
Pod documented
129 11 my $codec = $self->{ options }->{ codec };
130 11 my $data;
131
132 11 $self->debug("filesystem codec: $codec") if DEBUG;
133
134 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 5 6 11
135 5 ▶ $data = @_ == 1 ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 0 5
136 5 ▶ $data = $codec->encode($data) if $codec;
Branch: 1 branch
BranchTrueFalseTotal
if $codec 5 0 5
137 5 return $self->write($data);
138 }
139 else {
140 6 $data = $self->read;
141 6 ▶ $data = $codec->decode($data) if $codec;
Branch: 1 branch
BranchTrueFalseTotal
if $codec 6 0 6
142 6 return $data;
143 }
144 }
145  
146 sub accept {
147 196 ▶ $_[1]->visit_file($_[0]);
Subroutine accept: called CC=1 SCAR=0.0
Pod documented
148 }
149  
150 class->methods(
151 map {
152 70 my $item = $_; # lexical copy for closure
153 $item => sub {
154 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
155 # ...and strict in what you provide
156 2 $self->encoding(':' . $item);
157 2 return $self;
158 }
159 280 }
160 qw( raw utf8 crlf bytes )
161 );
162  
163  
164 70 1;
165  
166 __END__
« lib/Badger/Class/Config.pm ↑ Summary lib/Badger/Codec/Unicode.pm »