093ede6a00f85c0c3d8aa8fa41f24bc50e1ac36d
[scpubgit/DKit.git] / lib / DX / Op / ApplyConstraint.pm
1 package DX::Op::ApplyConstraint;
2
3 use Moo;
4
5 with 'DX::Role::Op';
6
7 has vars => (is => 'ro', required => 1);
8 has constraint => (is => 'ro', required => 1);
9
10 has _arg_map => (is => 'lazy', builder => sub {
11   my ($self) = @_;
12   my $name = 'arg0';
13   +{ map +($name++, $_), @{$self->vars} };
14 });
15
16 sub run {
17   my ($self, $state) = @_;
18   ($state, my %args) = $self->_expand_args($state, %{$self->_arg_map});
19   my @vars = map $_->bound_value, @args{sort keys %args};
20   if ($self->constraint->(@vars)) {
21     return $state->then($self->next);
22   }
23   return $state->backtrack;
24 }
25
26 1;