bugfixes, tweaks, map support
[dbsrgits/DBIx-Data-Store.git] / lib / DBIx / Data / Collection / Set / Mapped.pm
diff --git a/lib/DBIx/Data/Collection/Set/Mapped.pm b/lib/DBIx/Data/Collection/Set/Mapped.pm
new file mode 100644 (file)
index 0000000..7f2eb89
--- /dev/null
@@ -0,0 +1,43 @@
+package DBIx::Data::Collection::Set::Mapped;
+
+use strictures 1;
+
+use DBIx::Data::Stream::Mapped;
+
+sub new {
+  my $proto = shift;
+  bless({ %{$_[0]} }, ref($proto)||$proto);
+}
+
+sub _inner { shift->{inner} }
+sub _mapper { shift->{mapper} }
+
+sub flatten {
+  my ($self) = @_;
+  map $self->_do_map($_), $self->_inner->flatten;
+}
+
+sub to_stream {
+  my ($self) = @_;
+  my $mapper = $self->_mapper;
+  DBIx::Data::Stream::Mapped->new({
+    inner => $self->_inner->to_stream,
+    mapper => $self->_mapper
+  });
+}
+
+sub get {
+  my ($self, $spec) = @_;
+  if (my $got = $self->_inner->get($spec)) {
+    return $self->_do_map($got);
+  }
+  return undef;
+}
+
+sub _do_map {
+  my ($self, $to_map) = @_;
+  local $_ = $to_map;
+  $self->_mapper->($to_map);
+}
+
+1;