File Coverage

blib/lib/Directory/Scanner/Stream/Matching.pm
Criterion Covered Total %
statement 46 48 95.8
branch 7 10 70.0
condition 2 5 40.0
subroutine 14 15 93.3
pod 1 7 14.2
total 70 85 82.3


line stmt bran cond sub pod time code
1             package Directory::Scanner::Stream::Matching;
2             # ABSTRACT: Filtered streaming directory iterator
3              
4 4     4   20 use strict;
  4         7  
  4         90  
5 4     4   14 use warnings;
  4         8  
  4         84  
6              
7 4     4   13 use Carp ();
  4         6  
  4         36  
8 4     4   15 use Scalar::Util ();
  4         4  
  4         176  
9              
10             our $VERSION = '0.04';
11             our $AUTHORITY = 'cpan:STEVAN';
12              
13 4   50 4   16 use constant DEBUG => $ENV{DIR_SCANNER_STREAM_MATCHING_DEBUG} // 0;
  4         5  
  4         202  
14              
15             ## ...
16              
17 4     4   18 use parent 'UNIVERSAL::Object';
  4         7  
  4         32  
18 4     4   199 use roles 'Directory::Scanner::API::Stream';
  4         5  
  4         138  
19             use slots (
20             stream => sub {},
21             predicate => sub {},
22 4     4   1187 );
  4         7  
  4         25  
23              
24             ## ...
25              
26             sub BUILD {
27 7     7 1 370 my $self = $_[0];
28 7         17 my $stream = $self->{stream};
29 7         11 my $predicate = $self->{predicate};
30              
31 7 50 33     52 (Scalar::Util::blessed($stream) && $stream->roles::DOES('Directory::Scanner::API::Stream'))
32             || Carp::confess 'You must supply a directory stream';
33              
34 7 50       1068 (defined $predicate)
35             || Carp::confess 'You must supply a predicate';
36              
37 7 50       19 (ref $predicate eq 'CODE')
38             || Carp::confess 'The predicate supplied must be a CODE reference';
39             }
40              
41             sub clone {
42 0     0 0 0 my ($self, $dir) = @_;
43             return $self->new(
44             stream => $self->{stream}->clone( $dir ),
45             predicate => $self->{predicate}
46 0         0 );
47             }
48              
49             ## delegate
50              
51 21     21 0 817 sub head { $_[0]->{stream}->head }
52 29     29 0 3672 sub is_done { $_[0]->{stream}->is_done }
53 21     21 0 775 sub is_closed { $_[0]->{stream}->is_closed }
54 7     7 0 213 sub close { $_[0]->{stream}->close }
55              
56             sub next {
57 40     40 0 3584 my $self = $_[0];
58              
59 40         59 my $next;
60 40         43 while (1) {
61 70         530 undef $next; # clear any previous values, just cause ...
62 70         84 $self->_log('Entering loop ... ') if DEBUG;
63              
64 70         149 $next = $self->{stream}->next;
65              
66             # this means the stream is likely
67             # exhausted, so jump out of the loop
68 70 100       130 last unless defined $next;
69              
70             # now try to predicate the value
71             # and redo the loop if it does
72             # not pass
73 63         84 local $_ = $next;
74 63 100       136 next unless $self->{predicate}->( $next );
75              
76 33         471 $self->_log('Exiting loop ... ') if DEBUG;
77              
78             # if we have gotten to this
79             # point, we have a value and
80             # want to return it
81 33         53 last;
82             }
83              
84 40         81 return $next;
85             }
86              
87             1;
88              
89             __END__