1package Data::Stream::Bulk::Array;
2BEGIN {
3  $Data::Stream::Bulk::Array::AUTHORITY = 'cpan:NUFFIN';
4}
5{
6  $Data::Stream::Bulk::Array::VERSION = '0.11';
7}
8use Moose;
9# ABSTRACT: L<Data::Stream::Bulk> wrapper for simple arrays.
10
11use namespace::clean -except => 'meta';
12
13with qw(Data::Stream::Bulk) => { -excludes => [qw/loaded filter list_cat/] };
14
15has array => (
16	isa => "ArrayRef",
17	reader  => "_array",
18	writer  => "_set_array",
19	clearer => "_clear_array",
20	predicate => "_has_array",
21	required => 1,
22);
23
24sub is_done {
25	my $self = shift;
26	!$self->_has_array;
27}
28
29sub next {
30	my $self = shift;
31
32	if ( my $array = $self->_array ) {
33		$self->_clear_array;
34		return $array;
35	} else {
36		return;
37	}
38}
39
40# squish several arrays into one
41sub list_cat {
42	my ( $self, @rest ) = @_;
43
44	return $self unless @rest;
45
46	my @arrays = ( $self );
47
48	# fetch all adjacent arrays
49	push @arrays, shift @rest while @rest and $rest[0]->isa(__PACKAGE__);
50
51	if ( @arrays > 1 ) {
52		my @cat;
53		push @cat, @$_ for map { $_->_array } @arrays;
54		return __PACKAGE__->new(
55			array => \@cat,
56		)->cat( @rest );
57	} else {
58		my $head = shift @rest;
59		return ( $self, $head->list_cat(@rest) );
60	}
61}
62
63sub filter {
64	my ( $self, $filter ) = @_;
65	local $_ = $self->next;
66	$self->_set_array( $filter->($_) );
67	return $self;
68}
69
70sub loaded { 1 }
71
72__PACKAGE__->meta->make_immutable;
73
74__PACKAGE__;
75
76
77
78=pod
79
80=head1 NAME
81
82Data::Stream::Bulk::Array - L<Data::Stream::Bulk> wrapper for simple arrays.
83
84=head1 VERSION
85
86version 0.11
87
88=head1 SYNOPSIS
89
90	return Data::Stream::Bulk::Array->new(
91		array => \@results,
92	);
93
94=head1 DESCRIPTION
95
96This implementation of the L<Data::Stream::Bulk> api wraps an array.
97
98The use case is to keep the consumer of the data set implementation agnostic so
99that it can deal with larger data sets if they are encountered, but still
100retain most of the simplicity when the current data set easily fits in memory.
101
102=head1 ATTRIBUTES
103
104=over 4
105
106=item array
107
108The array reference to wrap.
109
110=back
111
112=head1 METHODS
113
114=over 4
115
116=item next
117
118Returns the array reference on the first invocation, and nothing thereafter.
119
120=item is_done
121
122Returns true if C<next> has been called.
123
124=item list_cat
125
126Squishes adjacent arrays into a new array.
127
128=item filter $filter
129
130Immediately applies C<$filter> to the internal array and returns C<$self>.
131
132=item loaded
133
134Returns true
135
136=back
137
138=head1 AUTHOR
139
140Yuval Kogman <nothingmuch@woobling.org>
141
142=head1 COPYRIGHT AND LICENSE
143
144This software is copyright (c) 2012 by Yuval Kogman.
145
146This is free software; you can redistribute it and/or modify it under
147the same terms as the Perl 5 programming language system itself.
148
149=cut
150
151
152__END__
153
154