e4dc28181736e6f79d7674f8510211b916858a33
[scpubgit/DKit.git] / lib / DX / Role / Op.pm
1 package DX::Role::Op;
2
3 use Moo::Role;
4
5 has next => (is => 'ro');
6
7 requires 'run';
8
9 sub but {
10   my ($self, @but) = @_;
11   $self->new(%$self, @but);
12 }
13
14 sub _expand_args {
15   my ($self, $state, %spec) = @_;
16   my %args;
17   foreach my $key (keys %spec) {
18     ($state, $args{$key}) = $self->_expand_argspec($state, $spec{$key});
19   }
20   return $state->expand_vars(%args);
21 }
22
23 sub _expand_argspec {
24   my ($self, $state, $spec) = @_;
25   if (!ref($spec)) {
26     ($state->has_scope_var($spec)
27       ? ($state, $state->scope_var($spec))
28       : (map { $_, $_->scope_var($spec) } $state->assign_vars($spec => {})));
29   } elsif (ref($spec) eq 'ARRAY') {
30     if ($spec->[0] eq 'value') {
31       ($state, +{ bound_value => $spec->[1] });
32     } else {
33       die "Arrayref in argspec is not value";
34     }
35   } elsif (ref($spec) eq 'SCALAR' or ref($spec) eq 'REF') {
36     return ($state, +{ bound_value => $$spec });
37   } else {
38     die "Argspec incomprehensible";
39   }
40 }
41
42
43 1;