File Coverage

blib/lib/PAUSE/Permissions/ModuleIterator.pm
Criterion Covered Total %
statement 57 58 98.2
branch 18 18 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 0 1 0.0
total 87 89 97.7


line stmt bran cond sub pod time code
1             package PAUSE::Permissions::ModuleIterator;
2             $PAUSE::Permissions::ModuleIterator::VERSION = '0.16';
3 6     6   20 use strict;
  6         7  
  6         132  
4 6     6   16 use warnings;
  6         5  
  6         130  
5 6     6   44 use 5.10.0;
  6         11  
6              
7 6     6   17 use Moo;
  6         3  
  6         22  
8 6     6   1365 use PAUSE::Permissions::Module;
  6         10  
  6         97  
9 6     6   2384 use autodie;
  6         64745  
  6         23  
10 6     6   21782 use feature 'state';
  6         11  
  6         3001  
11              
12             has 'permissions' =>
13             (
14             is => 'ro',
15             # isa => 'PAUSE::Permissions',
16             );
17              
18             has _fh => (is => 'rw');
19             has _cached_line => (is => 'rw', clearer => '_clear_cached_line');
20              
21             sub next_module
22             {
23 30     30 0 896 my $self = shift;
24 30         24 my $line;
25             my $current_module;
26 0         0 my $fh;
27              
28 30 100       82 if (not defined $self->_fh) {
29 5         23 $fh = $self->permissions->open_file();
30 5         7 my $inheader = 1;
31              
32             # Skip the header block at the top of the file
33 5         69 while (<$fh>) {
34 15 100       50 last if /^$/;
35             }
36 5         15 $self->_fh($fh);
37             }
38             else {
39 25         31 $fh = $self->_fh;
40             }
41              
42 30         27 my $perms = {};
43 30         25 while (1) {
44 90 100       129 if (defined $self->_cached_line) {
45 20         24 $line = $self->_cached_line;
46 20         225 $self->_clear_cached_line();
47             } else {
48 70         114 $line = <$fh>;
49             }
50              
51 90 100       1218 if (defined($line)) {
    100          
52 80         157 $line =~ s/[\r\n]+$//;
53 80         133 my ($module, $user, $permission) = split(/,/, $line);
54 80         75 $user = uc($user);
55 80 100 100     250 if (defined($current_module) && $module ne $current_module) {
56 20         28 $self->_cached_line($line);
57 20         18 my $module_name = $current_module;
58 20         15 $current_module = undef;
59 20         35 return $self->_new_module($module_name, $perms);
60             }
61 60         41 $current_module = $module;
62 60         44 push(@{ $perms->{ $permission } }, $user);
  60         110  
63             } elsif (defined $current_module) {
64 5         13 return $self->_new_module($current_module, $perms);
65             } else {
66 5         17 return undef;
67             }
68             }
69              
70             }
71              
72             sub _new_module
73             {
74 25     25   20 my $self = shift;
75 25         17 my $module_name = shift;
76 25         23 my $perms = shift;
77 25         33 my @args = (name => $module_name);
78              
79 25 100       58 push(@args, m => $perms->{m}->[0]) if exists($perms->{m});
80 25 100       47 push(@args, f => $perms->{f}->[0]) if exists($perms->{f});
81 25 100       42 push(@args, c => $perms->{c}) if exists($perms->{c});
82              
83 25         342 return PAUSE::Permissions::Module->new(@args);
84             }
85              
86             1;