factor out FindIsh, implement ForEach
[scpubgit/DKit.git] / lib / DX / Op / FindAll.pm
index 74caead..f8770fb 100644 (file)
@@ -7,54 +7,21 @@ use DX::OrderedSet;
 use DX::Op::Return;
 use Moo;
 
-with 'DX::Role::Op';
+with 'DX::Role::Op::FindIsh';
 
 has coll_name => (is => 'ro', required => 1);
 
-has var_name => (is => 'ro', required => 1);
-
-has body => (is => 'ro', required => 1);
-
-sub run {
-  my ($self, $state) = @_;
-  my $values = [];
-  my $coll = DX::OrderedSet->new(values => $values);
-  my $collect = DX::Op::FindAllCollect->new(
-    var_name => $self->var_name,
-    into => $values
-  );
-  my $do_body = DX::Op::FromCode->new(
+sub make_result_handler {
+  my ($self, $coll) = @_;
+  my $coll_name = $self->coll_name;
+  DX::Op::FromCode->new(
     code => sub {
       my ($self, $state) = @_;
-      $state->push_return_then($collect, $self->next);
+      $state->bind_value($state->scope->{$coll_name} => $coll)
+            ->then($self->next);
     },
-    next => $self->body
-  );
-  my $var = DX::Var->new(id => "rule:findall")
-                   ->with_stream(DX::ArrayStream->from_array(
-                       $do_body, DX::Op::Return->new
-                     ));
-  my $invoke = DX::Op::FromCode->new(
-    code => sub {
-      my ($self, $state) = @_;
-      my $op = $state->resolve_value($var);
-      $state->then($op);
-    }
-  );
-  my $coll_name = $self->coll_name;
-  my $ret = DX::Op::SetScope->new(
-    scope => $state->scope,
-    next => DX::Op::FromCode->new(
-      code => sub {
-        my ($self, $state) = @_;
-        $state->bind_value($state->scope->{$coll_name} => $coll)
-              ->then($self->next);
-      },
-      next => $self->next
-    )
+    next => $self->next
   );
-  $state->assign_vars($self->var_name => {})
-        ->push_return_then($ret, $invoke)->mark_choice($var);
 }
 
 1;