File Coverage

blib/lib/CPAN/Releases/Latest/DistributionIterator.pm
Criterion Covered Total %
statement 52 56 92.8
branch 16 18 88.8
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 1 0.0
total 79 87 90.8


line stmt bran cond sub pod time code
1             package CPAN::Releases::Latest::DistributionIterator;
2             $CPAN::Releases::Latest::DistributionIterator::VERSION = '0.06';
3 1     1   22 use 5.006;
  1         3  
  1         36  
4 1     1   5 use Moo;
  1         1  
  1         9  
5 1     1   296 use CPAN::Releases::Latest;
  1         2  
  1         24  
6 1     1   495 use CPAN::Releases::Latest::Distribution;
  1         2  
  1         27  
7 1     1   561 use CPAN::Releases::Latest::Release;
  1         2  
  1         654  
8              
9             has 'latest' =>
10             (
11             is => 'ro',
12             default => sub { CPAN::Releases::Latest->new() },
13             );
14              
15             has _fh => (is => 'rw');
16             has _previous_release => (is => 'rw', clearer => 1);
17              
18             sub next_distribution
19             {
20 6     6 0 1555 my $self = shift;
21 6         7 my $previous_release;
22              
23 6         24 $previous_release = $self->_previous_release();
24 6 50 66     41 if ( defined($previous_release)
25             && $previous_release->distinfo->maturity eq 'developer') {
26 0         0 $self->_clear_previous_release();
27 0         0 return _single_release_distribution($previous_release);
28             }
29              
30 6         102 my $release = $self->_next_release();
31 6 100       1564 if (defined($release)) {
    50          
32 5 100       65 if (defined($previous_release)) {
    100          
33 1         7 return $self->_dist_from_two_releases($previous_release, $release);
34             }
35             elsif ($release->distinfo->maturity eq 'developer') {
36 2         134 return _single_release_distribution($release);
37             } else {
38 2         162 my $next_release = $self->_next_release();
39 2         61 return $self->_dist_from_two_releases($release, $next_release);
40             }
41             }
42             elsif (defined($previous_release)) {
43 0         0 $self->_clear_previous_release();
44 0         0 return _single_release_distribution($previous_release);
45             }
46             else {
47 1         2 return undef;
48             }
49              
50             }
51              
52             sub _dist_from_two_releases
53             {
54 3     3   4 my $self = shift;
55 3         5 my $first_release = shift;
56 3         4 my $second_release = shift;
57              
58 3 100       13 if ($first_release->distname eq $second_release->distname) {
59 2         27 $self->_clear_previous_release();
60 2         589 return CPAN::Releases::Latest::Distribution->new(
61             distname => $first_release->distname,
62             release => $first_release,
63             developer_release => $second_release,
64             );
65             }
66             else {
67 1         4 $self->_previous_release($second_release);
68 1         3 return _single_release_distribution($first_release);
69             }
70             }
71              
72             sub _next_release
73             {
74 8     8   14 my $self = shift;
75 8         9 my $fh;
76              
77 8 100       27 if (not defined($fh = $self->_fh)) {
78 1         7 $fh = $self->latest->_open_file();
79 1         4 $self->_fh($fh);
80             }
81              
82 8         25 my $line = <$fh>;
83              
84 8 100       15 if (defined($line)) {
85 7         12 chomp($line);
86 7         25 my ($distname, $path, $time, $size) = split(/\s+/, $line);
87 7         156 return CPAN::Releases::Latest::Release->new(
88             distname => $distname,
89             path => $path,
90             timestamp => $time,
91             size => $size,
92             );
93             }
94             else {
95 1         3 return undef;
96             }
97              
98             }
99              
100             sub _single_release_distribution
101             {
102 3     3   6 my $release = shift;
103 3         61 my @args = (distname => $release->distinfo->dist);
104              
105 3 100       107 if ($release->distinfo->maturity eq 'developer') {
106 2         18 push(@args, developer_release => $release);
107             }
108             else {
109 1         11 push(@args, release => $release);
110             }
111 3         60 return CPAN::Releases::Latest::Distribution->new(@args);
112             }
113              
114             1;