Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Data / Stream / Bulk / Filter.pm
1 #!/usr/bin/perl
2
3 package Data::Stream::Bulk::Filter;
4 use Moose;
5
6 use Data::Stream::Bulk;
7
8 use namespace::clean -except => 'meta';
9
10 has filter => (
11         isa => "CodeRef",
12         reader => "filter_body",
13         required => 1,
14 );
15
16 has stream => (
17         does => "Data::Stream::Bulk",
18         is   => "ro",
19         required => 1,
20         handles  => [qw(is_done loaded)],
21 );
22
23 with qw(Data::Stream::Bulk) => { excludes => 'loaded' };
24
25 sub next {
26         my $self = shift;
27
28         local $_ = $self->stream->next;
29         return $_ && ( $self->filter_body->($_) || [] );
30 }
31
32 __PACKAGE__->meta->make_immutable;
33
34 __PACKAGE__
35
36 __END__
37
38 =pod
39
40 =head1 NAME
41
42 Data::Stream::Bulk::Filter - Streamed filtering (block oriented)
43
44 =head1 SYNOPSIS
45
46         use Data::Stream::Bulk::Filter;
47
48         Data::Stream::Bulk::Filter->new(
49                 filter => sub { ... },
50                 stream => $stream,
51         );
52
53 =head1 DESCRIPTION
54
55 This class implements filtering of streams.
56
57 =head1 ATTRIBUTES
58
59 =over 4
60
61 =item filter
62
63 The code reference to apply to each block.
64
65 The block is passed to the filter both in C<$_> and as the first argument.
66
67 The return value should be an array reference. If no true value is returned the
68 output stream does B<not> end, but instead an empty block is substituted (the
69 parent stream controls when the stream is depleted).
70
71 =item stream
72
73 The stream to be filtered
74
75 =back
76
77 =head1 METHODS
78
79 =over 4
80
81 =item is_done
82
83 =item loaded
84
85 Delegated to C<stream>
86
87 =item next
88
89 Calls C<next> on C<stream> and applies C<filter> if a block was returned.
90
91 =back
92
93 =cut
94
95