API/File/PackagesDetails.pm

filter: Statementstmt 91.5% 65 / 71 Branchbran 64.2% 9 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.2% 15 / 17 Podpod 60.0% 3 / 5 totaltotal 83.9% 94 / 112 CC 10 SCAR 12.7
CC
10
Cov
86%
parse
7.0
BUILDARGS
6.5
package
2.0
CRAP 3.5
« API/File/ModList.pm ↑ Summary API/File/MailRc.pm »
1 package CPAN::Index::API::File::PackagesDetails;
2 {
3 2 $CPAN::Index::API::File::PackagesDetails::VERSION = '0.007';
4 }
5  
6 # ABSTRACT: Interface to 02packages.details.txt
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Path::Class qw(file dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 with qw(
20 CPAN::Index::API::Role::Writable
21 CPAN::Index::API::Role::Readable
22 CPAN::Index::API::Role::Clonable
23 CPAN::Index::API::Role::HavingFilename
24 CPAN::Index::API::Role::HavingGeneratedBy
25 );
26  
27 2 has uri => (
28 is => 'ro',
29 isa => 'Str',
30 required => 1,
31 lazy_build => 1,
32 );
33  
34 2 has repo_uri => (
35 is => 'ro',
36 isa => 'Str',
37 );
38  
39 2 has description => (
40 is => 'ro',
41 isa => 'Str',
42 required => 1,
43 default => 'Package names found in directory $CPAN/authors/id/',
44 );
45  
46 2 has columns => (
47 is => 'ro',
48 isa => 'Str',
49 required => 1,
50 default => 'package name, version, path',
51 );
52  
53 2 has intended_for => (
54 is => 'ro',
55 isa => 'Str',
56 required => 1,
57 default => 'Automated fetch routines, namespace documentation.',
58 );
59  
60 has packages => (
61 is => 'bare',
62 isa => 'ArrayRef[HashRef]',
63 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 traits => ['Array'],
65 handles => {
66 package_count => 'count',
67 packages => 'elements',
68 add_package => 'push',
69 },
70 );
71  
72 sub BUILDARGS {
73 5 ▶ my ( $class, %args ) = @_;
Subroutine BUILDARGS: called CC=4 SCAR=18.7
Pod documented
74  
75 5 ▶ if ( $args{uri} or $args{repo_uri} )
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'uri'} or $args{'repo_uri'}) { } 5 0 5
elsif ($args{'repo_path'}) { } 0 0 0
Condition66%2/3
$args{"uri"} or $args{"repo_uri"}
l!l&&r!l&&!r
230
MC/DC0%0/2
$args{"uri"} or $args{"repo_uri"}
$args{"uri"} $args{"repo_uri"}
Truth table66%2/3
A or B
A= $args{"uri"}B= $args{"repo_uri"}
ABresult
1X1
011
000
76 {
77 5 return \%args;
78 }
79 elsif ($args{repo_path})
80 {
81 $args{repo_uri} = URI::file->new(
82 0 dir($args{repo_path})->absolute,
83 )->as_string;
84  
85 0 return \%args;
86 }
87 else
88 {
89 0 croak "Either 'uri', 'repo_uri' or 'repo_path' is required";
90 }
91 }
92  
93 sub _build_uri {
94 3 ▶ my $self = shift;
Subroutine _build_uri: called CC=1 SCAR=0.0
95 3 my $uri = URI->new($self->repo_uri);
96 $uri->path_segments(
97 3 grep { $_ ne '' } $uri->path_segments,
98 file($self->default_location)->dir->dir_list,
99 file($self->default_location)->basename,
100 );
101 3 return $uri->as_string;
102 }
103  
104 sub package
105 {
106 0 ▶ my ($self, $name) = @_;
Subroutine package: not called CC=1 SCAR=6.9
Pod undocumented
107 0 ▶ return first { $_->{name} eq $name } $self->packages;
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 }
109  
110 sub sorted_packages
111 {
112 3 ▶ my $self = shift;
Subroutine sorted_packages: called CC=1 SCAR=0.0
Pod undocumented
113 3 return sort { $a->{name} cmp $b->{name} } $self->packages;
114 }
115  
116 sub parse {
117 2 ▶ my ( $self, $content ) = @_;
Subroutine parse: called CC=7 SCAR=19.5
Pod documented
118  
119 2 my %map = (
120 'File' => 'file',
121 'URL' => 'uri',
122 'Description' => 'description',
123 'Columns' => 'columns',
124 'Intended-For' => 'intended_for',
125 'Written-By' => 'generated_by',
126 'Line-Count' => 'line_count',
127 'Last-Updated' => 'last_generated',
128 );
129  
130 2 my @lines = split "\n", $content;
131 2 my ( %args, @packages );
132  
133 2 ▶ while ( my $line = shift @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 16 2 18
134 16 ▶ last if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 16 16
135 16 ▶ next unless my ( $key, $value ) = $line =~ /^([^:]+):\s*(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless () = $line =~ /^([^:]+):\s*(.*)/ 0 16 16
136 16 $args{$map{$key}} = $value;
137 }
138  
139 2 foreach my $line ( @lines ) {
140 4 my ( $name, $version, $distribution ) = split ' ', $line;
141  
142 # normalize missing version
143 4 ▶ undef $version if $version eq 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $version eq "undef" 1 3 4
144  
145 4 my $package = {
146 name => $name,
147 version => $version,
148 distribution => $distribution,
149 };
150 4 push @packages, $package;
151 }
152  
153 2 ▶ $args{packages} = \@packages if @packages;
Branch: 1 branch
BranchTrueFalseTotal
if @packages 1 1 2
154  
155 2 return %args;
156 }
157  
158 11 ▶sub default_location { 'modules/02packages.details.txt.gz' }
Subroutine default_location: called CC=1 SCAR=0.0
Pod documented
159  
160 2 __PACKAGE__->meta->make_immutable;
161  
162  
163  
164  
165 =pod
166  
167 =head1 NAME
168  
169 CPAN::Index::API::File::PackagesDetails - Interface to 02packages.details.txt
170  
171 =head1 VERSION
172  
173 version 0.007
174  
175 =head1 SYNOPSIS
176  
177 my $pckdetails = CPAN::Index::File::PackagesDetails->parse_from_repo_uri(
178 'http://cpan.perl.org'
179 );
180  
181 foreach my $package ($pckdetails->packages) {
182 ... # do something
183 }
184  
185 =head1 DESCRIPTION
186  
187 This is a class to read and write 03modlist.data
188  
189 =head1 METHODS
190  
191 =head2 packages
192  
193 List of hashrefs representing packages indexed in the file. Each hashref
194 has the following structure:
195  
196 =over
197  
198 =item name
199  
200 Package name, e.g. C<Foo::Bar>.
201  
202 =item version
203  
204 Package version, e.g. C<0.001>.
205  
206 =item distribuiton
207  
208 Distribution the package belongs to, e.g. C<Foo-Bar-0.001>.
209  
210 =back
211  
212 =head2 package_count
213  
214 Number of packages indexed in the file.
215  
216 =head2 filename
217  
218 Name of this file - defaults to C<02packages.details.txt.gz>;
219  
220 =head2 description
221  
222 Short description of the file.
223  
224 =head2 intended_for
225  
226 Target consumers of the file.
227  
228 =head2 uri
229  
230 Absolute URI pointing to the file location.
231  
232 =head2 parse
233  
234 Parses the file and reurns its representation as a data structure.
235  
236 =head2 default_location
237  
238 Default file location - C<modules/02packages.details.txt.gz>.
239  
240 =head1 METHODS FROM ROLES
241  
242 =over
243  
244 =item <CPAN::Index::API::Role::Readable/read_from_string>
245  
246 =item <CPAN::Index::API::Role::Readable/read_from_file>
247  
248 =item <CPAN::Index::API::Role::Readable/read_from_tarball>
249  
250 =item <CPAN::Index::API::Role::Readable/read_from_repo_path>
251  
252 =item <CPAN::Index::API::Role::Readable/read_from_repo_uri>
253  
254 =item L<CPAN::Index::API::Role::Writable/tarball_is_default>
255  
256 =item L<CPAN::Index::API::Role::Writable/repo_path>
257  
258 =item L<CPAN::Index::API::Role::Writable/template>
259  
260 =item L<CPAN::Index::API::Role::Writable/content>
261  
262 =item L<CPAN::Index::API::Role::Writable/write_to_file>
263  
264 =item L<CPAN::Index::API::Role::Writable/write_to_tarball>
265  
266 =item L<CPAN::Index::API::Role::Clonable/clone>
267  
268 =item L<CPAN::Index::API::Role::HavingFilename/filename>
269  
270 =item L<CPAN::Index::API::Role::HavingGeneratedBy/generated_by>
271  
272 =item L<CPAN::Index::API::Role::HavingGeneratedBy/last_generated>
273  
274 =back
275  
276 =head1 AUTHOR
277  
278 Peter Shangov <pshangov@yahoo.com>
279  
280 =head1 COPYRIGHT AND LICENSE
281  
282 This software is copyright (c) 2012 by Venda, Inc..
283  
284 This is free software; you can redistribute it and/or modify it under
285 the same terms as the Perl 5 programming language system itself.
286  
287 =cut
288  
289  
290 __DATA__
291 File: [% $self->filename %]
292 URL: [% $self->uri %]
293 Description: [% $self->description %]
294 Columns: [% $self->columns %]
295 Intended-For: [% $self->intended_for %]
296 Written-By: [% $self->generated_by %]
297 Line-Count: [% $self->package_count %]
298 Last-Updated: [% $self->last_generated %]
299 [%
300 if ($self->package_count)
301 {
302 $OUT .= "\n";
303 foreach my $package ($self->sorted_packages) {
304 $OUT .= sprintf "%-34s %5s %s\n",
305 $package->{name},
306 defined $package->{version} ? $package->{version} : 'undef',
307 $package->{distribution};
308 }
309 }
310 else
311 {
312 $OUT .= ''; # keeps Text::Template happy
313 }
314 %]
« API/File/ModList.pm ↑ Summary API/File/MailRc.pm »