File Coverage

blib/lib/CPAN/Local/Distribution.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package CPAN::Local::Distribution;
2             {
3             $CPAN::Local::Distribution::VERSION = '0.010';
4             }
5              
6             # ABSTRACT: Base distribution class
7              
8 1     1   568 use strict;
  1         1  
  1         22  
9 1     1   5 use warnings;
  1         1  
  1         19  
10              
11 1     1   659 use Path::Class ();
  1         61961  
  1         17  
12 1     1   733 use CPAN::DistnameInfo;
  1         896  
  1         25  
13 1     1   843 use URI;
  1         4818  
  1         29  
14 1     1   1369 use Moose;
  0            
  0            
15             use namespace::clean -except => 'meta';
16              
17             has filename => ( is => 'ro', isa => 'Str', required => 1 );
18             has authorid => ( is => 'ro', isa => 'Str', required => 1 );
19             has path => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1 );
20              
21             around BUILDARGS => sub
22             {
23             my ( $orig, $class, %args ) = @_;
24              
25             # proceed as nomal if we already have authorid
26             return $class->$orig(%args) if $args{authorid};
27             my $path = Path::Class::file($args{filename});
28              
29             # calculate the path, e.g. ('authors', 'id', 'A', 'AD', 'ADAMK', 'File-Which-1.09.tar.gz')
30             my @path_parts = ( $path->dir->dir_list, $path->basename );
31              
32             # get the last 6 parts of the path
33             @path_parts = splice( @path_parts, -6 ) if @path_parts >= 6;
34              
35             # make sure we use only forward slashes
36             my $distname = Path::Class::file(@path_parts)->as_foreign('Unix')->stringify;
37              
38             # get the authorid
39             my $distnameinfo = CPAN::DistnameInfo->new($distname);
40             $args{authorid} = $distnameinfo->cpanid;
41              
42             return $class->$orig(%args);
43             };
44              
45             sub _build_path
46             {
47             my $self = shift;
48              
49             my $filename = Path::Class::file($self->filename)->basename;
50              
51             my @chars = split //, $self->authorid;
52             my $path = Path::Class::dir(
53             'authors',
54             'id',
55             $chars[0],
56             $chars[0] . $chars[1],
57             $self->authorid,
58             $filename,
59             );
60              
61             return $path->as_foreign('Unix')->stringify;
62             }
63              
64             __PACKAGE__->meta->make_immutable;
65              
66              
67             __END__
68             =pod
69              
70             =head1 NAME
71              
72             CPAN::Local::Distribution - Base distribution class
73              
74             =head1 VERSION
75              
76             version 0.010
77              
78             =head1 ATTRIBUTES
79              
80             =head2 filename
81              
82             The distribution filename, e.g. C<Foo-Bar-0.001.tar.gz>. Can (and often will)
83             contain the full path to the distribution (e.g.
84             C</path/to/distros/Foo-Bar-0.001.tar.gz>), but the last part must always be a
85             valid distribution filename. Required.
86              
87             =head2 authorid
88              
89             The id (a.k.a. CPAN id) of the distribution author. If not supplied, but
90             L</filename> is recognized as a full path that contains the authorid, it
91             will be extracted from there.
92              
93             =head2 path
94              
95             Distribution path relative to the repository root, e.g.
96             C<authors/id/F/FO/FOOBAR/Foo-Bar-0.001.tar.gz>. Normally does not need to be
97             supplied and will be calculated from the L</filename> and L</authorid>.
98              
99             =head1 AUTHOR
100              
101             Peter Shangov <pshangov@yahoo.com>
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is copyright (c) 2012 by Venda, Inc..
106              
107             This is free software; you can redistribute it and/or modify it under
108             the same terms as the Perl 5 programming language system itself.
109              
110             =cut
111