factor out FindIsh, implement ForEach
[scpubgit/DKit.git] / lib / DX / Op / OneEach.pm
1 package DX::Op::OneEach;
2
3 use DX::Op::FromCode;
4 use Moo;
5
6 with 'DX::Role::Op';
7
8 has var_name => (is => 'ro', required => 1);
9
10 has each_of => (is => 'ro', required => 1);
11
12 has each_body => (is => 'ro', required => 1);
13
14 sub run {
15   my ($self, $state) = @_;
16   my ($this, @rest) = @{$self->each_of};
17   my $next_op = (@rest ? $self->but(each_of => \@rest) : $self->next);
18   my $ret_op = DX::Op::SetScope->new(
19     scope => $state->scope, next => $next_op
20   );
21   $state->assign_vars($self->var_name => { bound_value => $this })
22         ->push_return_then($ret_op, $self->each_body);
23 }
24
25 1;