File Coverage

blib/lib/Directory/Scanner/StreamBuilder/Application.pm
Criterion Covered Total %
statement 39 41 95.1
branch 5 8 62.5
condition 2 5 40.0
subroutine 15 16 93.7
pod 1 7 14.2
total 62 77 80.5


line stmt bran cond sub pod time code
1             package Directory::Scanner::StreamBuilder::Application;
2             # ABSTRACT: Apply function to streaming directory iterator
3              
4 8     8   40 use strict;
  8         15  
  8         176  
5 8     8   33 use warnings;
  8         15  
  8         153  
6              
7 8     8   35 use Carp ();
  8         13  
  8         102  
8 8     8   28 use Scalar::Util ();
  8         156  
  8         117  
9              
10 8     8   33 use UNIVERSAL::Object;
  8         16  
  8         127  
11 8     8   29 use Directory::Scanner::API::Stream;
  8         15  
  8         362  
12              
13             our $VERSION = '0.01';
14             our $AUTHORITY = 'cpan:STEVAN';
15              
16 8   50 8   38 use constant DEBUG => $ENV{DIR_SCANNER_STREAM_APPLICATION_DEBUG} // 0;
  8         16  
  8         656  
17              
18             ## ...
19              
20 8     8   454 our @ISA; BEGIN { @ISA = ('UNIVERSAL::Object', 'Directory::Scanner::API::Stream') }
21             our %HAS; BEGIN {
22             %HAS = (
23             stream => sub {},
24             function => sub {},
25             )
26 8     8   1834 }
27              
28             ## ...
29              
30             sub BUILD {
31 2     2 1 141 my $self = $_[0];
32 2         6 my $stream = $self->{stream};
33 2         5 my $f = $self->{function};
34              
35 2 50 33     18 (Scalar::Util::blessed($stream) && $stream->DOES('Directory::Scanner::API::Stream'))
36             || Carp::confess 'You must supply a directory stream';
37              
38 2 50       7 (defined $f)
39             || Carp::confess 'You must supply a `function` value';
40              
41 2 50       8 (ref $f eq 'CODE')
42             || Carp::confess 'The `function` value supplied must be a CODE reference';
43             }
44              
45             sub clone {
46 0     0 0 0 my ($self, $dir) = @_;
47             return $self->new(
48             stream => $self->{stream}->clone( $dir ),
49             function => $self->{function}
50 0         0 );
51             }
52              
53             ## delegate
54              
55 9     9 0 747 sub head { $_[0]->{stream}->head }
56 4     4 0 9715 sub is_done { $_[0]->{stream}->is_done }
57 6     6 0 1172 sub is_closed { $_[0]->{stream}->is_closed }
58 2     2 0 193 sub close { $_[0]->{stream}->close }
59              
60             sub next {
61 12     12 0 2694 my $self = $_[0];
62 12         52 my $next = $self->{stream}->next;
63              
64             # this means the stream is likely exhausted
65 12 100       36 return unless defined $next;
66              
67             # apply the function ...
68 10         26 local $_ = $next;
69 10         34 $self->{function}->( $next );
70              
71             # return the next value
72 10         2452 return $next;
73             }
74              
75             1;
76              
77             __END__