File Coverage

blib/lib/Directory/Scanner/StreamBuilder/Ignoring.pm
Criterion Covered Total %
statement 45 47 95.7
branch 7 10 70.0
condition 2 5 40.0
subroutine 14 16 87.5
pod 1 7 14.2
total 69 85 81.1


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