File Coverage

lib/App/MtAws/QueueJobResult.pm
Criterion Covered Total %
statement 86 86 100.0
branch 45 56 80.3
condition 16 20 80.0
subroutine 17 17 100.0
pod 0 8 0.0
total 164 187 87.7


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::QueueJobResult;
22              
23             our $VERSION = '1.114_2';
24              
25 44     44   546644 use strict;
  44         72  
  44         1213  
26 44     44   168 use warnings;
  44         66  
  44         1076  
27              
28 44     44   169 use Carp;
  44         63  
  44         2338  
29 44     44   208 use Scalar::Util qw/blessed/;
  44         76  
  44         1904  
30 44     44   189 use Exporter 'import';
  44         59  
  44         1401  
31              
32 44     44   194 use constant JOB_RETRY => "MT_J_RETRY";
  44         75  
  44         3010  
33 44     44   204 use constant JOB_OK => "MT_J_OK";
  44         63  
  44         2279  
34 44     44   195 use constant JOB_WAIT => "MT_J_WAIT";
  44         70  
  44         1937  
35 44     44   176 use constant JOB_DONE => "MT_J_DONE";
  44         59  
  44         38179  
36              
37             our @EXPORT = qw/JOB_RETRY JOB_OK JOB_WAIT JOB_DONE state task job parse_result/;
38              
39             my @valid_codes_a = (JOB_RETRY, JOB_OK, JOB_WAIT, JOB_DONE);
40             my %valid_codes_h = map { $_ => 1 } @valid_codes_a;
41             our @valid_fields = qw/code default_code task state job/;
42              
43             ### Instance methods
44              
45             sub new
46             {
47 22146     22146 0 32054 my ($class, %args) = @_;
48 22146         19820 my $self = \%args;
49 22146         22734 bless $self, $class;
50 22146         25772 return $self;
51             }
52              
53             sub partial_new
54             {
55 10695     10695 0 29267 my ($class, %args) = @_;
56 10695         18552 my $self = $class->new(%args);
57 10695         12675 $self->{_type} = 'partial';
58 10695         32751 return $self;
59             }
60              
61             sub full_new
62             {
63 11451     11451 0 74837 my ($class, %args) = @_;
64 11451         19495 my $self = $class->new(%args);
65 11451         14256 $self->{_type} = 'full';
66 11451         16275 return $self;
67             }
68              
69             ### Class methods and DSL
70              
71             sub is_code($)
72             {
73 10432     10432 0 24900 $valid_codes_h{shift()};
74             }
75              
76              
77             # state STATE
78             # returns: list with 2 elements
79             sub state($)
80             {
81 3502     3502 0 9672 my $class = __PACKAGE__;
82 3502 50       5996 confess unless wantarray;
83             return
84 3502         6432 $class->partial_new(state => shift),
85             $class->partial_new(default_code => JOB_RETRY);
86              
87             }
88              
89             # job JOB
90             # returns: list with 2 elements
91             sub job(@)
92             {
93 311     311 0 2455 my ($job, $cb) = @_;
94 311 50       566 confess unless wantarray;
95             return
96 311 100       1158 JOB_RETRY,
97             __PACKAGE__->partial_new(job => { job => $job, $cb ? (cb => $cb) : () } );
98             }
99              
100             # task ACTION, sub { ... }
101             # task ACTION, { k1 => v1, k2 => v2 ... }, sub { ... }
102             # task ACTION, { k1 => v1, k2 => v2 ... }, \$ATTACHMENT, sub { ... }
103             # returns: list with 2 elements
104             sub task(@)
105             {
106 3330 50   3330 0 322285 confess unless wantarray;
107 3330         3299 my $class = __PACKAGE__;
108 3330 100       5927 confess "at least two args expected" unless @_ >= 2;
109 3329         5233 my ($task_action, $cb, $task_args, $attachment) = (shift, pop, @_);
110              
111 3329 100       7241 if (ref $task_action eq ref {}) {
112 517         429 my $h = $task_action;
113 517 100       1083 ($task_action, $task_args, $attachment) = ($h->{action}, $h->{args}, $h->{attachment} ? $h->{attachment} : ());
114             }
115              
116              
117 3329 100 100     14454 confess "task_args should be hashref" if defined($task_args) && (ref($task_args) ne ref({}));
118 3328 50       5548 confess "no task action" unless $task_action;
119 3328 100 66     11552 confess "no code ref" unless $cb && ref($cb) eq 'CODE';
120 3326 100 100     6632 confess "attachment is not reference to scalar: ".ref($attachment) if defined($attachment) && (ref($attachment) ne ref(\""));
121             return
122 3325 100 100     15613 JOB_OK,
123             $class->partial_new(task => {
124             action => $task_action, cb => $cb, args => $task_args||{}, defined($attachment) ? ( attachment => $attachment) : ()
125             });
126             }
127              
128              
129             =pod
130              
131             parse_result(@) input is a list concatenation of one or more of the following entities: TASK, JOB, STATE and CODE
132              
133             TASK - is a return value of task() function. (i.e. list with 2 items - task object and CODE)
134             JOB - is a return value of job() function (i.e. list with 2 items - job object and CODE)
135             STATE - is a return value of state() function (i.e. list with 2 items - state object and default_code object)
136             CODE - is JOB_xxx code
137              
138             allowed combinations:
139              
140             STATE
141             [STATE, ] (TASK|JOB)
142             [STATE, ] CODE (when CODE is not JOB_OK )
143              
144             =cut
145              
146             sub parse_result
147             {
148 10485     10485 0 23882 my $class = __PACKAGE__;
149 10485         10052 my $res = {};
150 10485 100       18054 confess "no data" unless @_;
151 10484         12283 for my $o (@_) {
152 19100 100 66     79798 if (blessed($o) && $o->isa($class)) { # anything, but code
    100          
153 10632 100       17930 confess "should be partial" unless $o->{_type} eq 'partial';
154 10631         11665 my @fields_to_copy = grep { $o->{$_} } @valid_fields;
  53155         57414  
155 10631 50       16667 confess "should be just one field in the object" if @fields_to_copy != 1;
156 10631         10444 my ($field_to_copy) = @fields_to_copy;
157 10631 100       16673 confess "double data: $field_to_copy" if defined($res->{$field_to_copy});
158 10626         21425 $res->{$field_to_copy} = $o->{$field_to_copy};
159             } elsif (ref($o) eq ref("")) { # code
160 8466 100       18700 confess "code already exists" if defined($res->{code});
161 8414         14649 $res->{code} = $o;
162             } else {
163 2         247 confess "bad argument: $o";
164             }
165             }
166              
167 10424   66     19090 $res->{code} ||= $res->{default_code};
168 10424         9805 delete $res->{default_code};
169              
170 10424         26511 $res = $class->full_new(%$res);
171 10424 50       23087 confess "no code" unless defined($res->{code});
172 10424 50       15611 confess "code is false" unless $res->{code};
173 10424 100       13511 confess "bad code" unless is_code $res->{code};
174 10423 100       17506 if ($res->{code} eq JOB_OK) {
175 3310 100       5676 confess "no task" unless defined($res->{task});
176 3305 50       5270 confess "no task action" unless defined($res->{task}{action});
177 3305 50       5045 confess "no task cb" unless defined($res->{task}{cb});
178 3305 50       5242 confess "no task args" unless defined($res->{task}{args});
179             }
180 10418 50 66     30528 confess "unexpected task" if ($res->{code} ne JOB_OK && defined($res->{task}));
181 10418         19219 $res;
182             }
183              
184              
185             1;