File Coverage

lib/Badger/Filesystem/File.pm
Criterion Covered Total %
statement 51 55 92.7
branch 6 12 50.0
condition n/a
subroutine 19 21 90.4
pod 19 19 100.0
total 95 107 88.7


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Filesystem::File
4             #
5             # DESCRIPTION
6             # OO representation of a file in a filesystem.
7             #
8             # AUTHOR
9             # Andy Wardley
10             #
11             #========================================================================
12              
13             package Badger::Filesystem::File;
14              
15             use Badger::Class
16 70         543 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     70   803 };
  70         131  
26              
27              
28             # aliases
29             *base = \&directory;
30             *copy = \©_to;
31             *move = \&move_to;
32              
33              
34             sub init {
35 295     295 1 501 my ($self, $config) = @_;
36 295         783 $self->init_path($config);
37 295         938 $self->init_options($config);
38 295         1051 return $self;
39             }
40              
41             sub directory {
42 5     5 1 7 my $self = shift;
43             return @_
44 5 50       25 ? $self->filesystem->directory( $self->relative(@_) )
45             : $self->parent;
46             }
47              
48             sub file {
49 0     0 1 0 my $self = shift;
50             return @_
51 0 0       0 ? $self->filesystem->file( $self->relative(@_) )
52             : $self;
53             }
54              
55             sub exists {
56 74     74 1 234 my $self = shift;
57             # cache the stats returned in case we want them later
58 74         170 return ($self->{ stats } = $self->filesystem->file_exists($self->{ path }));
59             }
60              
61             sub create {
62 2     2 1 7 my $self = shift;
63 2         6 $self->filesystem->create_file($self->{ path }, @_);
64             }
65              
66             sub touch {
67 2     2 1 4 my $self = shift;
68 2         7 $self->filesystem->touch_file($self->{ path });
69             }
70              
71             sub open {
72 3     3 1 11 my $self = shift;
73 3         7 $self->filesystem->open_file($self->{ path }, @_, $self->{ options });
74             }
75              
76             sub read {
77 29     29 1 48 my $self = shift;
78 29         81 $self->filesystem->read_file($self->{ path }, @_, $self->{ options });
79             }
80              
81             sub write {
82 12     12 1 75 my $self = shift;
83 12         39 $self->filesystem->write_file($self->{ path }, @_, $self->{ options });
84             }
85              
86             sub append {
87 4     4 1 12 my $self = shift;
88 4         14 $self->filesystem->append_file($self->{ path }, @_, $self->{ options });
89             }
90              
91             sub copy_to {
92 2     2 1 8 my $self = shift;
93 2         4 $self->filesystem->copy_file($self->{ path }, @_);
94             }
95              
96             sub copy_from {
97 1     1 1 2 my $self = shift;
98 1         4 $self->filesystem->copy_file(shift, $self->{ path }, @_);
99             }
100              
101             sub move_to {
102 1     1 1 4 my $self = shift;
103 1         6 $self->filesystem->move_file($self->{ path }, @_);
104             }
105              
106             sub move_from {
107 0     0 1 0 my $self = shift;
108 0         0 $self->filesystem->move_file(shift, $self->{ path }, @_);
109             }
110              
111             sub print {
112 2     2 1 4 my $self = shift;
113 2         10 $self->write( join(BLANK, @_) );
114             }
115              
116             sub delete {
117 13     13 1 84 my $self = shift;
118 13         41 $self->filesystem->delete_file($self->{ path }, @_);
119             }
120              
121             sub text {
122 18     18 1 121 my $self = shift;
123 18         64 my $text = $self->read(@_, $self->{ options });
124 18         121 return $text;
125             }
126              
127             sub data {
128 11     11 1 38 my $self = shift;
129 11         23 my $codec = $self->{ options }->{ codec };
130 11         12 my $data;
131            
132 11         15 $self->debug("filesystem codec: $codec") if DEBUG;
133            
134 11 100       22 if (@_) {
135 5 50       11 $data = @_ == 1 ? shift : [@_];
136 5 50       32 $data = $codec->encode($data) if $codec;
137 5         303 return $self->write($data);
138             }
139             else {
140 6         17 $data = $self->read;
141 6 50       40 $data = $codec->decode($data) if $codec;
142 6         118 return $data;
143             }
144             }
145              
146             sub accept {
147 196     196 1 417 $_[1]->visit_file($_[0]);
148             }
149              
150             class->methods(
151             map {
152             my $item = $_; # lexical copy for closure
153             $item => sub {
154 2     2   11 my $self = shift;
155             # ...and strict in what you provide
156 2         11 $self->encoding(':' . $item);
157 2         6 return $self;
158             }
159             }
160             qw( raw utf8 crlf bytes )
161             );
162              
163              
164             1;
165              
166             __END__