| 1 | package TaskPipe::InterpParam::MatchHandler; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Clone 'clone'; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | has input => (is => 'rw', isa => 'HashRef', required => 1); | ||
| 8 | has input_history => (is => 'rw', isa => 'ArrayRef', required => 1); | ||
| 9 | has param => (is => 'rw', isa => 'HashRef', required => 1); | ||
| 10 | has param_history => (is => 'rw', isa => 'ArrayRef', required => 1); | ||
| 11 | has parts => (is => 'rw', isa => 'TaskPipe::InterpParam::Parts', required => 1); | ||
| 12 | |||
| 13 | sub name{ my ($n) = ref($_[0]) =~ /^${\__PACKAGE__}_(\w+)$/; $n; } | ||
| 14 | |||
| 15 | sub interp{ | ||
| 16 | my $self = shift; | ||
| 17 | |||
| 18 | my $i = $self->match_index; | ||
| 19 | |||
| 20 | return undef unless defined $i; | ||
| 21 | |||
| 22 | $i += $self->parts->match_offset; | ||
| 23 | $i += $self->match_adjustment if $self->can('match_adjustment'); | ||
| 24 | |||
| 25 | return undef if ($i > scalar(@{$self->param_history}) || $i < 0 ); | ||
| 26 | |||
| 27 | my $input_i; | ||
| 28 | |||
| 29 | if ( $i == 0 ){ | ||
| 30 | |||
| 31 | $input_i = $self->input; | ||
| 32 | |||
| 33 | } else { | ||
| 34 | |||
| 35 | $input_i = $self->input_history->[ $i - 1 ]; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | my $key = $self->parts->input_key; | ||
| 40 | |||
| 41 | if ( $key && $key eq '*' ){ | ||
| 42 | return +clone $input_i; | ||
| 43 | } | ||
| 44 | |||
| 45 | $key ||= $self->parts->param_key; | ||
| 46 | |||
| 47 | confess "Can't determine which input to get interp result from (no key name specified)" unless $key; | ||
| 48 | |||
| 49 | return $input_i->{$key}; | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | sub match_index{ | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | my $match_count = $self->parts->match_count; | ||
| 59 | |||
| 60 | my @param = ( $self->param, @{$self->param_history} ); | ||
| 61 | |||
| 62 | my $match_i; | ||
| 63 | for my $i (0..$#param){ | ||
| 64 | |||
| 65 | if ( $self->match_condition( $param[$i] ) ){ | ||
| 66 | |||
| 67 | if ( $match_count == 0 ){ | ||
| 68 | $match_i = $i; | ||
| 69 | last; | ||
| 70 | } | ||
| 71 | |||
| 72 | $match_count--; | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | return $match_i; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | sub match_condition{ | ||
| 83 | my ($self,$param) = @_; | ||
| 84 | |||
| 85 | confess "No name" unless $self->name; | ||
| 86 | my $pval = $param->{'_'.$self->name}; | ||
| 87 | $pval && $pval eq $self->parts->label_val?1:0; | ||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub format_valid{ | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | my $valid = 1; | ||
| 96 | $valid = 0 unless $self->parts->label_val; | ||
| 97 | return $valid; | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | =head1 NAME | ||
| 102 | |||
| 103 | TaskPipe::InterpParam::MatchHandler - handling matching of parameter values in the plan | ||
| 104 | |||
| 105 | =head1 DESCRIPTION | ||
| 106 | |||
| 107 | L<TaskPipe::MatchHandler> is the base class responsible for interpolating parameters in the plan which are set as a particular variable (e.g. C<$this>). To create a match handler for a new variable, this class should be inherited from. The minimum package to create a new plan variable is the empty package which inherits from L<TaskPipe::MatchHandler>: | ||
| 108 | |||
| 109 | package TaskPipe::InterpParam::MatchHandler_myvar; | ||
| 110 | use Moose; | ||
| 111 | extends 'TaskPipe::InterpParam::MatchHandler'; | ||
| 112 | 1; | ||
| 113 | |||
| 114 | This defines a parameter variable C<$myvar> which behaves just like C<$id>. The variable is taken from the name of the package. | ||
| 115 | |||
| 116 | If you add this module and TaskPipe sees C<$myvar> in the plan, it will look for the label C<_myvar> and replace the input in the same way as it would for C<_id>. | ||
| 117 | |||
| 118 | Why would you want to do this? I have no idea. It's probably more likely that, if you are wanting to create a new parameter variable, you want it to have some unique behaviour. | ||
| 119 | |||
| 120 | In this case you may want to override one or more of the methods C<match_condition>, C<format_valid> and/or C<match_index>. | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | Tom Gracey <tomgracey@gmail.com> | ||
| 125 | |||
| 126 | =head1 COPYRIGHT AND LICENSE | ||
| 127 | |||
| 128 | Copyright (c) Tom Gracey 2018 | ||
| 129 | |||
| 130 | TaskPipe is free software, licensed under | ||
| 131 | |||
| 132 | The GNU Public License Version 3 | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | __PACKAGE__->meta->make_immutable; | ||
| 137 | |||
| 138 | 1; |