factor some code-based stream types out into classes
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / StreamBase.pm
CommitLineData
5f74b883 1package HTML::Zoom::StreamBase;
2
3use strict;
4use warnings FATAL => 'all';
d80786d0 5use HTML::Zoom::MatchWithoutFilter;
6
7sub _zconfig { shift->{_zconfig} }
5f74b883 8
3cdbc13f 9sub peek {
10 my ($self) = @_;
11 if (exists $self->{_peeked}) {
12 return ($self->{_peeked});
13 }
14 if (my ($peeked) = $self->next) {
15 return ($self->{_peeked} = $peeked);
16 }
17 return;
18}
19
20sub flatten {
8a1c87d1 21 my $self = shift;
22 require HTML::Zoom::FlattenedStream;
23 HTML::Zoom::FlattenedStream->new({
24 source => $self,
25 zconfig => $self->_zconfig
3cdbc13f 26 });
27}
28
29sub map {
8a1c87d1 30 my ($self, $mapper) = @_;
31 require HTML::Zoom::MappedStream;
32 HTML::Zoom::MappedStream->new({
33 source => $self, mapper => $mapper, zconfig => $self->_zconfig
3cdbc13f 34 });
35}
36
d80786d0 37sub with_filter {
38 my ($self, $selector, $filter) = @_;
39 my $match = $self->_parse_selector($selector);
40 $self->_zconfig->stream_utils->wrap_with_filter($self, $match, $filter);
41}
42
43sub select {
44 my ($self, $selector) = @_;
45 my $match = $self->_parse_selector($selector);
46 return HTML::Zoom::MatchWithoutFilter->construct(
47 $self, $match, $self->_zconfig->filter_builder,
48 );
49}
50
51sub _parse_selector {
52 my ($self, $selector) = @_;
53 return $selector if ref($selector); # already a match sub
54 $self->_zconfig->selector_parser->parse_selector($selector);
55}
56
1c4455ae 57sub apply {
58 my ($self, $code) = @_;
59 local $_ = $self;
60 $self->$code;
61}
62
5f74b883 631;