Giddy/StaticDirectory.pm

filter: Statementstmt 36.9% 17 / 46 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 40.0% 4 / 10 Podpod 100% 6 / 6 totaltotal 31.7% 27 / 85 CC 11 SCAR 25.6
CC
11
Cov
26%
get_static_dir
20.0
open_text_file
20.0
open_binary_file
20.0
CRAP 13.0
« Giddy/Role/DocumentStorer.pm ↑ Summary Giddy/Role/PathAnalyzer.pm »
1 package Giddy::StaticDirectory;
2  
3 # ABSTRACT: A Giddy directory of static files.
4  
5 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "0.013_001";
12 2 $VERSION = eval $VERSION;
13  
14 =head1 NAME
15  
16 Giddy::StaticDirectory - A Giddy directory of static files.
17  
18 =head1 VERSION
19  
20 version 0.013_001
21  
22 =head1 SYNOPSIS
23  
24 my $dir = $coll->get_static_dir('static_files');
25  
26 my $fh = $dir->open_text_file("robots.txt");
27 $fh->print("User-agent: *\nDisallow:");
28 $fh->close;
29  
30 $db->stage('static_files');
31 $db->commit('create a static-file directory');
32  
33 =head1 DESCRIPTION
34  
35 This class represents Giddy static-file directories, which are directories that
36 contain files which aren't documents and do not change often, most probably
37 binary files (pictures, videos, songs) and other files which are often called
38 "static" in the world of websites. Any sub-directory in a static directory is
39 also considered a static directory, with infinite nestability.
40  
41 =head1 ATTRIBUTES
42  
43 =head2 path
44  
45 The relative path of the directory. Always has a starting slash. Required.
46  
47 =head2 coll
48  
49 The L<Giddy::Collection> object the directory belongs to. This is even if the
50 directory is not a direct child of the collection, but some descendant of it.
51 Required.
52  
53 =cut
54  
55 2 has 'path' => (is => 'ro', isa => 'Str', required => 1);
56  
57 2 has 'coll' => (is => 'ro', isa => 'Giddy::Collection', required => 1);
58  
59 =head1 OBJECT METHODS
60  
61 =head2 list_files()
62  
63 Returns a list of all static files in the directory.
64  
65 =cut
66  
67 0 ▶sub list_files {
Subroutine list_files: not called CC=1 SCAR=6.9
Pod documented
68 0 my $self = shift;
69  
70 0 return $self->coll->db->_list_files($self->path);
71 }
72  
73 =head2 list_dirs()
74  
75 Returns a list of all child directories in the directory. These are all considered
76 static-file directories as well.
77  
78 =cut
79  
80 0 ▶sub list_dirs {
Subroutine list_dirs: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82  
83 0 return $self->coll->db->_list_dirs($self->path);
84 }
85  
86 =head3 get_static_dir( $name )
87  
88 Returns a Giddy::StaticDirectory object for a child directory of the current
89 directory named C<$name>. If the directory does not exist, it will be created.
90 If a file named C<$name> exists in the directory, this method will croak.
91  
92 =cut
93  
94 0 ▶sub get_static_dir {
Subroutine get_static_dir: not called CC=4 SCAR=30.0
Pod documented
95 0 my ($self, $name) = @_;
96  
97 0 ▶ croak "You must provide the name of the static directory to load."
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
98 unless $name;
99  
100 0 my $fpath = $self->path.'/'.$name;
101  
102 # try to find such a directory
103 0 ▶ if ($self->coll->db->_path_exists($fpath)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->coll->db->_path_exists($fpath)) { } 0 0 0
104 0 ▶ if ($self->coll->db->_is_directory($fpath)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->coll->db->_is_directory($fpath)) { } 0 0 0
105 # we don't check if the directory is a static directory, since by
106 # definition a descendant of a static directory is a static directory
107 0 return Giddy::StaticDirectory->new(path => $fpath, _collection => $self);
108 } else {
109 0 croak "A file named $name exists in the ".$self->path." static directory, there cannot be a directory named like that as well.";
110 }
111 } else {
112 # okay, let's create the directory
113 0 $self->coll->db->_create_dir($fpath);
114 0 $self->coll->db->stage($fpath);
115  
116 0 return Giddy::StaticDirectory->new(path => $fpath, _collection => $self);
117 }
118 }
119  
120 =head2 open_text_file( $name, [ $mode ] )
121  
122 Creates a new file named C<$name> (if not exists) inside the static directory,
123 opens it and returns a L<IO::File> object handle. If C<$mode> is not provided,
124 C<< '>:utf8' >> is used (meaning the file is truncated (if exists) and opened
125 for writing with automatic UTF-8 encoding of written data). You can provide other
126 modes if you wish, but keep in mind that Giddy expects all your files to be UTF-8.
127  
128 =cut
129  
130 0 ▶sub open_text_file {
Subroutine open_text_file: not called CC=4 SCAR=30.0
Pod documented
131 0 my ($self, $name, $mode) = @_;
132  
133 0 ▶ croak "You must provide the name of the text file to open."
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
134 unless $name;
135  
136 0 ▶ $mode ||= '>:utf8';
Condition0%0/2
$mode ||= ">:utf8"
l!l
00
MC/DC0%0/1
$mode ||= ">:utf8"
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
137  
138 0 my $fpath = $self->coll->db->_repo->work_tree.'/'.$self->path.'/'.$name;
139  
140 0 ▶ IO::File->new($fpath, $mode) || croak "Can't open text file $name: $!";
Branch: 1 branch
BranchTrueFalseTotal
"IO::File"->new($fpath, $mode) or croak("Can't open text file ${name}: $!") 0 0 0
141 }
142  
143 =head2 open_binary_file( $name, [ $mode, $layer ] )
144  
145 Creates a new file named C<$name> (if not exists) inside the static directory,
146 opens it, C<binmode>s it and returns a L<IO::File> object handle. If C<$mode>
147 isn't provided, C<< '>' >> is used (meaning the file is truncated (if exists)
148 and opened for writing). If your provide a layer string is provided, it will be
149 passed to C<binmode()> when calling it.
150  
151 =cut
152  
153 0 ▶sub open_binary_file {
Subroutine open_binary_file: not called CC=4 SCAR=30.0
Pod documented
154 0 my ($self, $name, $mode, $layer) = @_;
155  
156 0 ▶ croak "You must provide the name of the binary file to open."
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
157 unless $name;
158  
159 0 ▶ $mode ||= '>';
Condition0%0/2
$mode ||= ">"
l!l
00
MC/DC0%0/1
$mode ||= ">"
$mode
Truth table0%0/2
A
A= $mode
Aresult
11
00
160  
161 0 my $fpath = $self->coll->db->_repo->work_tree.'/'.$self->path.'/'.$name;
162  
163 0 ▶ my $fh = IO::File->new($fpath, $mode) || croak "Can't open binary file $name: $!";
Condition0%0/2
'IO::File'->new($fpath, $mode) || croak("Can't open binary file ${name}: $!")
l!l
00
MC/DC0%0/1
'IO::File'->new($fpath, $mode) || croak("Can't open binary file ${name}: $!")
'IO::File'->new($fpath, $mode)
Truth table0%0/2
A
A= 'IO::File'->new($fpath, $mode)
Aresult
11
00
164 0 $fh->binmode($layer);
165 0 return $fh;
166 }
167  
168 =head2 read_file( $name )
169  
170 Returns the contents of the file named C<$name> in the static directory. Assumes
171 the file exists and has been indexed, will croak if not.
172  
173 =cut
174  
175 0 ▶sub read_file {
Subroutine read_file: not called CC=2 SCAR=17.9
Pod documented
176 0 my ($self, $name) = @_;
177  
178 0 ▶ croak "You must provide the name of the file to read."
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
179 unless $name;
180  
181 0 return $self->coll->db->_read_content($self->path.'/'.$name);
182 }
183  
184 =head1 AUTHOR
185  
186 Ido Perlmuter, C<< <ido at ido50.net> >>
187  
188 =head1 BUGS
189  
190 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
191 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
192 automatically be notified of progress on your bug as I make changes.
193  
194 =head1 SUPPORT
195  
196 You can find documentation for this module with the perldoc command.
197  
198 perldoc Giddy::StaticDirectory
199  
200 You can also look for information at:
201  
202 =over 4
203  
204 =item * RT: CPAN's request tracker
205  
206 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
207  
208 =item * AnnoCPAN: Annotated CPAN documentation
209  
210 L<http://annocpan.org/dist/Giddy>
211  
212 =item * CPAN Ratings
213  
214 L<http://cpanratings.perl.org/d/Giddy>
215  
216 =item * Search CPAN
217  
218 L<http://search.cpan.org/dist/Giddy/>
219  
220 =back
221  
222 =head1 LICENSE AND COPYRIGHT
223  
224 Copyright 2011 Ido Perlmuter.
225  
226 This program is free software; you can redistribute it and/or modify it
227 under the terms of either: the GNU General Public License as published
228 by the Free Software Foundation; or the Artistic License.
229  
230 See http://dev.perl.org/licenses/ for more information.
231  
232 =cut
233  
234 2 __PACKAGE__->meta->make_immutable;
« Giddy/Role/DocumentStorer.pm ↑ Summary Giddy/Role/PathAnalyzer.pm »