Giddy/Role/PathAnalyzer.pm

filter: Statementstmt 20.8% 10 / 48 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 16.6% 2 / 12 Podpod n/a totaltotal 14.8% 12 / 81 CC 10 SCAR 21.1
CC
10
Cov
15%
_path_exists
20.0
_is_static_dir
12.0
_list_contents
6.0
CRAP 8.3
« Giddy/StaticDirectory.pm ↑ Summary Giddy.pm »
1 package Giddy::Role::PathAnalyzer;
2  
3 2 ▶use Any::Moose 'Role';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.013_001";
7 2 $VERSION = eval $VERSION;
8  
9 =head1 NAME
10  
11 Giddy::Role::PathAnalyzer - Provides common path analysis methods to Giddy::Database
12  
13 =head1 VERSION
14  
15 version 0.013_001
16  
17 =head1 SYNOPSIS
18  
19 # used internally
20  
21 =head1 DESCRIPTION
22  
23 This role provides L<Giddy::Database> with common methods needed for analyzing
24 paths in the database.
25  
26 Requires the attribute '_repo' to be implemented by consuming classes.
27  
28 =cut
29  
30 2 requires '_repo';
31  
32 =head1 METHODS
33  
34 =head2 _list_contents( $path )
35  
36 Returns a list of all files and directories in C<$path>. Assumes C<$path> is a directory.
37  
38 =cut
39  
40 0 ▶sub _list_contents {
Subroutine _list_contents: not called CC=2 SCAR=17.9
41 0 my ($self, $path) = @_;
42  
43 0 ▶ return grep { $_ ne '.giddy' } sort $self->_repo->run('ls-tree', '--name-only', $path ? "HEAD:$path" : 'HEAD');
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 0 0 0
44 }
45  
46 =head2 _list_files( $path )
47  
48 Returns a list of all static files in the directory. Assumes C<$path> is a directory.
49  
50 =cut
51  
52 0 ▶sub _list_files {
Subroutine _list_files: not called CC=1 SCAR=6.9
53 0 my ($self, $path) = @_;
54  
55 0 return grep { $self->_is_file($path.'/'.$_) } $self->_list_contents($path);
56 }
57  
58 =head2 _list_dirs( $path )
59  
60 Returns a list of all child directories in the directory. Assumes C<$path> is a directory.
61  
62 =cut
63  
64 0 ▶sub _list_dirs {
Subroutine _list_dirs: not called CC=1 SCAR=6.9
65 0 my ($self, $path) = @_;
66  
67 0 return grep { $self->_is_directory($path.'/'.$_) } $self->_list_contents($path);
68 }
69  
70 =head2 _read_content( $path )
71  
72 Returns the contents of the file stored in C<$path>.
73  
74 =cut
75  
76 0 ▶sub _read_content {
Subroutine _read_content: not called CC=1 SCAR=6.9
77 0 my ($self, $path) = @_;
78  
79 0 ''.$self->_repo->run('show', "HEAD:$path");
80 }
81  
82 =head2 _path_exists( $path )
83  
84 Returns true if C<$path> exists in the database index (i.e. it's not enough for
85 the path to exist in the working directory, it must be in the Git index as well).
86  
87 =cut
88  
89 0 ▶sub _path_exists {
Subroutine _path_exists: not called CC=4 SCAR=30.0
90 0 my ($self, $path) = @_;
91  
92 0 my ($dir, $name) = ('', '');
93 0 ▶ if ($path && $path =~ m!/([^/]+)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and $path =~ m[/([^/]+)$]) { } 0 0 0
Condition0%0/3
$path and $path =~ m[/([^/]+)$]
!ll&&!rl&&r
000
MC/DC0%0/2
$path and $path =~ m[/([^/]+)$]
$path $path =~ m[/([^/]+)$]
Truth table0%0/3
A and B
A= $pathB= $path =~ m[/([^/]+)$]
ABresult
0X0
100
111
94 0 $name = $1;
95 0 $dir = $`;
96 } else {
97 0 $name = $path;
98 }
99  
100 0 ▶ if (grep { $_ eq $name } $self->_list_contents($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $name;} $self->_list_contents($dir)) 0 0 0
101 0 return 1;
102 }
103  
104 0 return;
105 }
106  
107 =head2 _is_file( $path )
108  
109 Returns true if C<$path> is a file. Assumes path exists.
110  
111 =cut
112  
113 0 ▶sub _is_file {
Subroutine _is_file: not called CC=2 SCAR=17.9
114 0 my ($self, $path) = @_;
115  
116 0 my $t = $self->_repo->run('cat-file', '-t', "HEAD:$path");
117 0 ▶ return $t eq 'blob' ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'blob' ? : 0 0 0
118 }
119  
120 =head2 _is_directory( $path )
121  
122 Returns true if C<$path> is a directory. Assumes path exists.
123  
124 =cut
125  
126 0 ▶sub _is_directory {
Subroutine _is_directory: not called CC=2 SCAR=17.9
127 0 my ($self, $path) = @_;
128  
129 0 my $t = $self->_repo->run('cat-file', '-t', "HEAD:$path");
130 0 ▶ return $t eq 'tree' ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'tree' ? : 0 0 0
131 }
132  
133 =head2 _is_document_dir( $path )
134  
135 Returns true if C<$path> is a document directory. Assumes path exists.
136  
137 =cut
138  
139 0 ▶sub _is_document_dir {
Subroutine _is_document_dir: not called CC=1 SCAR=6.9
140 0 my ($self, $path) = @_;
141  
142 0 return $self->_path_exists($path.'/'.'attributes.yaml');
143 }
144  
145 =head2 _is_static_dir( $path )
146  
147 Returns true if C<$path> is a static-file directory. Assumes path exists.
148  
149 =cut
150  
151 0 ▶sub _is_static_dir {
Subroutine _is_static_dir: not called CC=3 SCAR=24.8
152 0 my ($self, $path) = @_;
153  
154 # a static dir is marked by a .static file, but it doesn't have to have
155 # that file if it's a child of a static dir
156 0 ▶ while ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
157 0 ▶ return 1 if $self->_path_exists($path.'/.static');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_path_exists($path . '/.static') 0 0 0
158 0 $path = $self->_up($path);
159 }
160  
161 0 return;
162 }
163  
164 =head2 _up( $path )
165  
166 Returns the parent directory of C<$path> (if any).
167  
168 =cut
169  
170 0 ▶sub _up {
Subroutine _up: not called CC=2 SCAR=17.9
171 0 my ($self, $path) = @_;
172  
173 0 ▶ if ($path =~ m!/[^/]+$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/[^/]+$]) { } 0 0 0
174 0 return $`;
175 } else {
176 0 return '';
177 }
178 }
179  
180 =head1 AUTHOR
181  
182 Ido Perlmuter, C<< <ido at ido50.net> >>
183  
184 =head1 BUGS
185  
186 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
187 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
188 automatically be notified of progress on your bug as I make changes.
189  
190 =head1 SUPPORT
191  
192 You can find documentation for this module with the perldoc command.
193  
194 perldoc Giddy::Role::PathAnalyzer
195  
196 You can also look for information at:
197  
198 =over 4
199  
200 =item * RT: CPAN's request tracker
201  
202 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
203  
204 =item * AnnoCPAN: Annotated CPAN documentation
205  
206 L<http://annocpan.org/dist/Giddy>
207  
208 =item * CPAN Ratings
209  
210 L<http://cpanratings.perl.org/d/Giddy>
211  
212 =item * Search CPAN
213  
214 L<http://search.cpan.org/dist/Giddy/>
215  
216 =back
217  
218 =head1 LICENSE AND COPYRIGHT
219  
220 Copyright 2011 Ido Perlmuter.
221  
222 This program is free software; you can redistribute it and/or modify it
223 under the terms of either: the GNU General Public License as published
224 by the Free Software Foundation; or the Artistic License.
225  
226 See http://dev.perl.org/licenses/ for more information.
227  
228 =cut
229  
230 2 1;
« Giddy/StaticDirectory.pm ↑ Summary Giddy.pm »