allow value scalarref, refactor
[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   @args{keys %spec} = map $self->_expand_argspec($state, $_), values %spec;
18   return $state->expand_vars(%args);
19 }
20
21 sub _expand_argspec {
22   my ($self, $state, $spec) = @_;
23   if (!ref($spec)) {
24     $state->scope_var($spec)
25   } elsif (ref($spec) eq 'ARRAY') {
26     if ($spec->[0] eq 'value') {
27       +{ bound_value => $spec->[1] };
28     } else {
29       die "Arrayref in argspec is not value";
30     }
31   } elsif (ref($spec) eq 'SCALAR' or ref($spec) eq 'REF') {
32     return +{ bound_value => $$spec };
33   } else {
34     die "Argspec incomprehensible";
35   }
36 }
37
38
39 1;