Zapp/Task/Action.pm

filter: Statementstmt 29.6% 8 / 27 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 28.5% 2 / 7 Podpod 80.0% 4 / 5 totaltotal 31.1% 14 / 45 CC 4 SCAR 17.2
CC
4
Cov
24%
execute
12.0
action_field
6.0
prepare
2.0
CRAP 5.6
« Test/Zapp.pm ↑ Summary Zapp/Type.pm »
1 package Zapp::Task::Action;
2  
3 #pod =head1 SYNOPSIS
4 #pod
5 #pod package My::Task::Action;
6 #pod use Mojo::Base 'Zapp::Task::Action', -signatures;
7 #pod
8 #pod sub prepare( $self, $input ) {
9 #pod # Notify user of input
10 #pod ...;
11 #pod # Call superclass to show input form on run page
12 #pod $self->SUPER::prepare( $self, $input );
13 #pod }
14 #pod
15 #pod sub action( $self, $task_input, $c, $form_input ) {
16 #pod # Process form input
17 #pod return $self->SUPER::action( $task_input, $c, $form_input );
18 #pod }
19 #pod
20 #pod sub run( $self, $task_input ) {
21 #pod my $form_input = $self->info->{notes}{input};
22 #pod # Do something with input
23 #pod ...;
24 #pod # Call superclass to finish
25 #pod $self->SUPER::run( $c, $task_input, $form_input );
26 #pod }
27 #pod
28 #pod 1;
29 #pod __DATA__
30 #pod @@ action.html.ep
31 #pod %# Display form for user input
32 #pod
33 #pod =head1 DESCRIPTION
34 #pod
35 #pod Actions are L<Task classes|Zapp::Task> that prompt for user input and
36 #pod then wait. While they are waiting, Action classes may display a button,
37 #pod an input form, or other content on the run page.
38 #pod
39 #pod Unlike regular Tasks, custom Actions have three steps:
40 #pod
41 #pod =over
42 #pod
43 #pod =item prepare
44 #pod
45 #pod The L<prepare step|/prepare> is run by a Worker. This can be extended to perform any
46 #pod necessary notifications or setup. Once the action is prepared, the user may see
47 #pod the L<action_field|/action_field> on the Run page.
48 #pod
49 #pod =item action
50 #pod
51 #pod The L<action step|/action> is run by the web application when a user
52 #pod interacts with the L<action field|/action_field>. The form input is
53 #pod stored in the C<input> note (see L<Minion::Job/info>).
54 #pod
55 #pod =item run
56 #pod
57 #pod The L<run step|/run> is run by the worker after the user has interacted
58 #pod with the L<action field|/action_field>. The default behavior is to
59 #pod simply finish the job successfully with the user's input. Override this
60 #pod for custom behavior.
61 #pod
62 #pod =back
63 #pod
64 #pod =head1 SEE ALSO
65 #pod
66 #pod L<Zapp::Task::Action::Confirm>, L<Zapp::Task>, L<Zapp>
67 #pod
68 #pod =cut
69  
70 3 ▶use Mojo::Base 'Zapp::Task', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶use Mojo::Loader qw( data_section );
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 3 my $DELAY = 60*60*24*365; # Next year
74  
75 ▶sub execute( $self ) {
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
76 0 my $notes = $self->info->{notes};
77 0 ▶ return $self->SUPER::execute if $notes->{input};
Branch: 1 branch
BranchTrueFalseTotal
if $notes->{"input"} 0 0 0
78 0 ▶ if ( $self->zapp_task->{state} ne 'waiting' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zapp_task->{"state"} ne "waiting") 0 0 0
79 0 $self->app->log->info( 'Preparing action ' . $self->zapp_task->{task_id} );
80 0 return $self->prepare( @{ $self->args } );
81 }
82 0 $self->app->log->info( 'Tried to run waiting action before input' );
83 0 return $self->retry({ delay => $DELAY });
84 }
85  
86 ▶sub prepare( $self, $task_input ) {
Subroutine prepare: not called CC=1 SCAR=6.9
Pod documented
87 # Delay re-running the job for a while
88 0 $self->retry({ delay => $DELAY });
89 # Update the Zapp status to display the action form
90 0 $self->set(
91 state => 'waiting',
92 );
93 0 return;
94 }
95  
96 ▶sub action_field( $self, $c, $task_input ) {
Subroutine action_field: not called CC=2 SCAR=17.9
Pod undocumented
97 0 my $class = blessed $self;
98 0 my $tmpl = data_section( $class, 'action.html.ep' );
99 0 ▶ return '' if !$tmpl;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpl 0 0 0
100 # XXX: Use Mojo::Template directly to get better names than 'inline
101 # template XXXXXXXXXXXX'?
102 0 return $c->render_to_string(
103 inline => $tmpl,
104 self => $self,
105 input => $task_input,
106 );
107 }
108  
109 ▶sub action( $self, $c, $task_input, $form_input ) {
Subroutine action: not called CC=1 SCAR=6.9
Pod documented
110 0 $self->note( input => $form_input );
111 0 $self->set( state => 'inactive' );
112 0 $self->retry({ delay => -1 });
113 }
114  
115 ▶sub run( $self, $task_input ) {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
116 0 $self->finish( $self->notes->{input} );
117 }
118  
119 3 1;
120  
121 =pod
122  
123 =head1 NAME
124  
125 Zapp::Task::Action
126  
127 =head1 VERSION
128  
129 version 0.004
130  
131 =head1 SYNOPSIS
132  
133 package My::Task::Action;
134 use Mojo::Base 'Zapp::Task::Action', -signatures;
135  
136 sub prepare( $self, $input ) {
137 # Notify user of input
138 ...;
139 # Call superclass to show input form on run page
140 $self->SUPER::prepare( $self, $input );
141 }
142  
143 sub action( $self, $task_input, $c, $form_input ) {
144 # Process form input
145 return $self->SUPER::action( $task_input, $c, $form_input );
146 }
147  
148 sub run( $self, $task_input ) {
149 my $form_input = $self->info->{notes}{input};
150 # Do something with input
151 ...;
152 # Call superclass to finish
153 $self->SUPER::run( $c, $task_input, $form_input );
154 }
155  
156 1;
157 __DATA__
158 @@ action.html.ep
159 %# Display form for user input
160  
161 =head1 DESCRIPTION
162  
163 Actions are L<Task classes|Zapp::Task> that prompt for user input and
164 then wait. While they are waiting, Action classes may display a button,
165 an input form, or other content on the run page.
166  
167 Unlike regular Tasks, custom Actions have three steps:
168  
169 =over
170  
171 =item prepare
172  
173 The L<prepare step|/prepare> is run by a Worker. This can be extended to perform any
174 necessary notifications or setup. Once the action is prepared, the user may see
175 the L<action_field|/action_field> on the Run page.
176  
177 =item action
178  
179 The L<action step|/action> is run by the web application when a user
180 interacts with the L<action field|/action_field>. The form input is
181 stored in the C<input> note (see L<Minion::Job/info>).
182  
183 =item run
184  
185 The L<run step|/run> is run by the worker after the user has interacted
186 with the L<action field|/action_field>. The default behavior is to
187 simply finish the job successfully with the user's input. Override this
188 for custom behavior.
189  
190 =back
191  
192 =head1 SEE ALSO
193  
194 L<Zapp::Task::Action::Confirm>, L<Zapp::Task>, L<Zapp>
195  
196 =head1 AUTHOR
197  
198 Doug Bell <preaction@cpan.org>
199  
200 =head1 COPYRIGHT AND LICENSE
201  
202 This software is copyright (c) 2021 by Doug Bell.
203  
204 This is free software; you can redistribute it and/or modify it under
205 the same terms as the Perl 5 programming language system itself.
206  
207 =cut
208  
209 __DATA__
« Test/Zapp.pm ↑ Summary Zapp/Type.pm »