package DBIx::Data::Stream::Mapped;

use strictures 1;

sub new {
  my $proto = shift;
  bless({ %{$_[0]} }, ref($proto)||$proto);
}

sub _inner { shift->{inner} }
sub _clear_inner { delete shift->{inner} }
sub _mapper { shift->{mapper} }

sub next {
  return unless my $inner = (my $self = shift)->_inner;
  # If we were aiming for a "true" perl-like map then we should
  # elegantly handle the case where the map function returns 0 events
  # and the case where it returns >1 - if you're reading this comment
  # because you wanted it to do that, now would be the time to fix it :)
  my $mapper = $self->_mapper;
  if (my ($next) = $inner->next) {
    local $_ = $next;
    return $mapper->($next);
  }
  $self->_clear_inner;
  return
}

1;
