move to TransformBuilder in Zoom
[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 }
b5a48c47 14 if (my ($peeked) = $self->_next) {
3cdbc13f 15 return ($self->{_peeked} = $peeked);
16 }
17 return;
18}
19
b5a48c47 20sub next {
21 my ($self) = @_;
22
23 # peeked entry so return that
24
25 if (exists $self->{_peeked}) {
26 return (delete $self->{_peeked});
27 }
28
29 $self->_next;
30}
31
32
3cdbc13f 33sub flatten {
8a1c87d1 34 my $self = shift;
35 require HTML::Zoom::FlattenedStream;
36 HTML::Zoom::FlattenedStream->new({
37 source => $self,
38 zconfig => $self->_zconfig
3cdbc13f 39 });
40}
41
42sub map {
8a1c87d1 43 my ($self, $mapper) = @_;
44 require HTML::Zoom::MappedStream;
45 HTML::Zoom::MappedStream->new({
46 source => $self, mapper => $mapper, zconfig => $self->_zconfig
3cdbc13f 47 });
48}
49
d80786d0 50sub with_filter {
51 my ($self, $selector, $filter) = @_;
52 my $match = $self->_parse_selector($selector);
53 $self->_zconfig->stream_utils->wrap_with_filter($self, $match, $filter);
54}
55
56sub select {
57 my ($self, $selector) = @_;
58 my $match = $self->_parse_selector($selector);
59 return HTML::Zoom::MatchWithoutFilter->construct(
60 $self, $match, $self->_zconfig->filter_builder,
61 );
62}
63
64sub _parse_selector {
65 my ($self, $selector) = @_;
66 return $selector if ref($selector); # already a match sub
67 $self->_zconfig->selector_parser->parse_selector($selector);
68}
69
1c4455ae 70sub apply {
71 my ($self, $code) = @_;
72 local $_ = $self;
73 $self->$code;
74}
75
5f74b883 761;