File Coverage

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


line stmt bran cond sub pod time code
1             package PAUSE::Permissions::ModuleIterator;
2             $PAUSE::Permissions::ModuleIterator::VERSION = '0.17';
3 6     6   43 use strict;
  6         15  
  6         193  
4 6     6   38 use warnings;
  6         44  
  6         199  
5 6     6   90 use 5.10.0;
  6         23  
6              
7 6     6   36 use Moo;
  6         14  
  6         94  
8 6     6   2095 use PAUSE::Permissions::Module;
  6         17  
  6         117  
9 6     6   2803 use autodie;
  6         83497  
  6         33  
10 6     6   39526 use feature 'state';
  6         19  
  6         3609  
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 1443 my $self = shift;
24 30         94 my $line;
25             my $current_module;
26 30         0 my $fh;
27              
28 30 100       116 if (not defined $self->_fh) {
29 5         37 $fh = $self->permissions->open_file();
30 5         16 my $inheader = 1;
31              
32             # Skip the header block at the top of the file
33 5         86 while (<$fh>) {
34 15 100       75 last if /^$/;
35             }
36 5         25 $self->_fh($fh);
37             }
38             else {
39 25         55 $fh = $self->_fh;
40             }
41              
42 30         59 my $perms = {};
43 30         62 while (1) {
44 90 100       275 if (defined $self->_cached_line) {
45 20         49 $line = $self->_cached_line;
46 20         365 $self->_clear_cached_line();
47             } else {
48 70         231 $line = <$fh>;
49             }
50              
51 90 100       345 if (defined($line)) {
    100          
52 80         339 $line =~ s/[\r\n]+$//;
53 80         301 my ($module, $user, $permission) = split(/,/, $line);
54 80         171 $user = uc($user);
55 80 100 100     411 if (defined($current_module) && $module ne $current_module) {
56 20         76 $self->_cached_line($line);
57 20         39 my $module_name = $current_module;
58 20         45 $current_module = undef;
59 20         64 return $self->_new_module($module_name, $perms);
60             }
61 60         108 $current_module = $module;
62 60         114 push(@{ $perms->{ $permission } }, $user);
  60         189  
63             } elsif (defined $current_module) {
64 5         17 return $self->_new_module($current_module, $perms);
65             } else {
66 5         27 return undef;
67             }
68             }
69              
70             }
71              
72             sub _new_module
73             {
74 25     25   53 my $self = shift;
75 25         48 my $module_name = shift;
76 25         44 my $perms = shift;
77 25         67 my @args = (name => $module_name);
78              
79 25 100       91 push(@args, m => $perms->{m}->[0]) if exists($perms->{m});
80 25 100       83 push(@args, f => $perms->{f}->[0]) if exists($perms->{f});
81 25 100       79 push(@args, c => $perms->{c}) if exists($perms->{c});
82              
83 25         457 return PAUSE::Permissions::Module->new(@args);
84             }
85              
86             1;