add basic inflation wrapper and simple inflator
[dbsrgits/DBIx-Data-Store.git] / lib / DBIx / Data / Stream / Mapped.pm
diff --git a/lib/DBIx/Data/Stream/Mapped.pm b/lib/DBIx/Data/Stream/Mapped.pm
new file mode 100644 (file)
index 0000000..0e2e77b
--- /dev/null
@@ -0,0 +1,29 @@
+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;