Task/Sequence.pm untested

filter: Statementstmt 0.0% 0 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 34 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
execute
2.0
base_task
2.0
CRAP 2.0
1  
2 =head1 NAME
3  
4 Util::Task::Sequence - A task for running other tasks sequentially.
5  
6 =head1 SYNOPSIS
7  
8 my $base_task = Util::Task::Something->new();
9 my $task = Util::Task::Sequence->new($base_task, sub {
10 my $result = shift;
11 return Util::Task::SomethingElse->new($result);
12 });
13  
14 =head1 DESCRIPTION
15  
16 In some cases, a task is actually a sequence of smaller task where each
17 step depends on the previous step. For example, if you want to load
18 a list of items for a user by username, you might first need to translate
19 the username into a userid.
20  
21 While it is in theory possible to build a single task that does both
22 of these steps, tasks that perform multiple steps are difficult to
23 batch and coalesce.
24  
25 This class, which is one of the fundamental building blocks of the
26 L<Util::Task> framework and is handled as a special case, allows
27 single tasks to be strung together into a sequence where the result
28 of one feeds into the next.
29  
30 When sequence tasks are added to a L<Util::Task::Multi>, the execution
31 of the multi-task is split into phases, each of which has its own batching
32 step. Where the constituent tasks support coalescing, the system will also
33 use solutions found in previous phases to avoid repeating work.
34  
35 =head1 USAGE
36  
37 The constructor for this class takes two arguments. The first is a L<Util::Task>
38 instance that will form the first step in this sequence. The second is
39 a CODE ref that will recieve the result returned by the first step and should
40 return either another L<Util::Task> instance for the second step or C<undef>
41 to indicate that the next step does not need to run and that C<undef> should
42 be returned for this overall task.
43  
44 The progression function may itself return a L<Util::Task::Sequence> to
45 allow multi-step sequences to be created dynamically.
46  
47 The return value of a sequence task is the return value of the task
48 returned by the progression function, or C<undef> if the progression
49 function returns C<undef>.
50  
51 =cut
52  
53 package Util::Task::Sequence;
54  
55 use strict;
56 use warnings;
57 use base qw(Util::Task);
58  
59 sub new {
60 my ($class, $base_task, $progress_function) = @_;
61  
62 my $self = bless {}, $class;
63 $self->{base_task} = $base_task;
64 $self->{progress_function} = $progress_function;
65 return $self;
66 }
67  
68 sub execute {
69 my ($self) = @_;
70  
71 # This simple execute implementation is provided to satisfy the Task
72 # interface, but this class is also handled by the execute() implementation
73 # in Util::Task::Multi.
74  
75 my $result1 = $self->{base_task}->execute();
76 my $next_task = $self->{progress_function}->($result1);
77 return $next_task->execute();
78 }
79  
80 sub base_task {
81 return $_[0]->{base_task};
82 }
83  
84 sub progression_function {
85 return $_[0]->{progress_function};
86 }
87  
88 # If this ever gets called directly then someone's doing something wrong.
89 # This class is handled as a special case by Util::Task::Multi.
90 sub execute_multi {
91 Carp::croak("Shouldn't call Util::Task::Sequence->execute_multi directly");
92 }
93  
94 1;