Jifty/Result.pm

filter: Statementstmt 13.2% 11 / 83 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 17.6% 3 / 17 Podpod 100% 12 / 12 totaltotal 15.5% 26 / 167 CC 27 SCAR 27.8
CC
27
Cov
8%
field_error
30.0
as_hash
30.0
_recurse_object_to_data
30.0
CRAP 16.1
« Jifty/Plugin.pm ↑ Summary Jifty/Web/Form/Field/Collection.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Result;
5  
6 =head1 NAME
7  
8 Jifty::Result - Outcome of running a L<Jifty::Action>
9  
10 =head1 DESCRIPTION
11  
12 C<Jifty::Result> encapsulates the outcome of running a
13 L<Jifty::Action>. Results are also stored on the framework's
14 L<Jifty::Response> object.
15  
16 =cut
17  
18  
19  
20 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 __PACKAGE__->mk_accessors(qw(failure action_class message _content));
23  
24  
25 =head2 new
26  
27 Construct a new action result. This is done automatically when the
28 action is created, and can be accessed via the
29 L<Jifty::Action/result>.
30  
31 =cut
32  
33 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
34 0 my $class = shift;
35 0 my $self = bless {}, $class;
36  
37 0 $self->failure(0);
38 0 $self->_content({});
39  
40 0 return $self;
41 }
42  
43 =head2 failure [BOOL]
44  
45 Gets or sets if the action succeeded or failed.
46  
47 =head2 success [BOOL]
48  
49 Gets or sets if the action succeeded or failed -- this is an
50 alternate interface from C<failure> but has the same effect.
51  
52 =cut
53  
54 0 ▶sub success {
Subroutine success: not called CC=2 SCAR=17.9
Pod documented
55 0 my $self = shift;
56 0 ▶ return 0 if $self->failure(map {not $_} @_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->failure(map({not $_;} @_)) 0 0 0
57 0 return 1;
58 }
59  
60 =head2 action_class [MESSAGE]
61  
62 Returns the class for the action that this result came from.
63  
64 =head2 message [MESSAGE]
65  
66 Gets or sets the action's response message. This is an informational
67 textual description of the outcome of the action.
68  
69 =head2 error [ERROR]
70  
71 Gets or sets the action's error response. This is an informational
72 textual description of what went wrong with the action, overall. This
73 also automatically sets the result to be a L</failure>.
74  
75 =cut
76  
77 0 ▶sub error {
Subroutine error: not called CC=4 SCAR=30.0
Pod documented
78 0 my $self = shift;
79
80 0 ▶ $self->failure(1) if @_ and $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $_[0] 0 0 0
Condition0%0/3
@_ and $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $_[0]
@_ $_[0]
Truth table0%0/3
A and B
A= @_B= $_[0]
ABresult
0X0
100
111
81 0 ▶ $self->{error} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
82 0 return $self->{error};
83 }
84  
85 =head2 field_error FIELD [ERROR] [OPTIONS]
86  
87 Gets or sets the error string for a specific field on the action.
88 This also automatically sets the result to be a failure. C<OPTIONS>
89 is an optional set of key-value pairs; the only currently supported
90 option is C<force>, which sets the L</ajax_force_validate> for this
91 field.
92  
93 =cut
94  
95 0 ▶sub field_error {
Subroutine field_error: not called CC=5 SCAR=34.0
Pod documented
96 0 my $self = shift;
97 0 my $field = shift;
98  
99 0 ▶ $self->failure(1) if @_ and $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $_[0] 0 0 0
Condition0%0/3
@_ and $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and $_[0]
@_ $_[0]
Truth table0%0/3
A and B
A= @_B= $_[0]
ABresult
0X0
100
111
100 0 ▶ $self->{field_errors}{ $field } = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
101  
102 0 my %args = @_;
103 0 ▶ $self->{ajax_force_validate}{ $field } = $args{force} if exists $args{force};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"force"} 0 0 0
104  
105 0 return $self->{field_errors}{ $field };
106 }
107  
108 =head2 field_errors
109  
110 Returns a hash which maps L<argument|Jifty::Manual::Glossary/argument>
111 name to error.
112  
113 =cut
114  
115 0 ▶sub field_errors {
Subroutine field_errors: not called CC=2 SCAR=17.9
Pod documented
116 0 my $self = shift;
117 0 ▶ return %{$self->{field_errors} || {}};
Condition0%0/2
$self->{'field_errors'} || {}
l!l
00
MC/DC0%0/1
$self->{'field_errors'} || {}
$self->{'field_errors'}
Truth table0%0/2
A
A= $self->{'field_errors'}
Aresult
11
00
118 }
119  
120 =head2 field_warning FIELD [WARNING] [OPTIONS]
121  
122 Gets or sets the warning string for a specific field on the
123 action. C<OPTIONS> is an optional set of key-value pairs; the only
124 currently supported option is C<force>, which sets the
125 L</ajax_force_validate> for this field.
126  
127 =cut
128  
129 0 ▶sub field_warning {
Subroutine field_warning: not called CC=3 SCAR=24.8
Pod documented
130 0 my $self = shift;
131 0 my $field = shift;
132  
133 0 ▶ $self->{field_warnings}{ $field } = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
134  
135 0 my %args = @_;
136 0 ▶ $self->{ajax_force_validate}{ $field } = $args{force} if exists $args{force};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"force"} 0 0 0
137  
138 0 return $self->{field_warnings}{ $field };
139 }
140  
141 =head2 field_warnings
142  
143 Returns a hash which maps L<argument|Jifty::Manual::Glossary/argument>
144 name to warning.
145  
146 =cut
147  
148 0 ▶sub field_warnings {
Subroutine field_warnings: not called CC=2 SCAR=17.9
Pod documented
149 0 my $self = shift;
150 0 ▶ return %{$self->{field_warnings} || {}};
Condition0%0/2
$self->{'field_warnings'} || {}
l!l
00
MC/DC0%0/1
$self->{'field_warnings'} || {}
$self->{'field_warnings'}
Truth table0%0/2
A
A= $self->{'field_warnings'}
Aresult
11
00
151 }
152  
153 =head2 ajax_force_validate FIELD [VALUE]
154  
155 Gets or sets the flag which determines if warnings and errors are set
156 using ajax validation, even if the field is empty. By default,
157 validation warnings and errors are I<not> shown for empty fields, as
158 yelling to users about mandatory fields they've not gotten to yet is
159 poor form. You can use this method to force ajax errors to show even
160 on empty fields.
161  
162 =cut
163  
164 0 ▶sub ajax_force_validate {
Subroutine ajax_force_validate: not called CC=2 SCAR=17.9
Pod documented
165 0 my $self = shift;
166 0 my $field = shift;
167 0 ▶ $self->{ajax_force_validate}{ $field } = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
168 0 return $self->{ajax_force_validate}{$field};
169 }
170  
171 =head2 field_canonicalization_note FIELD [NOTE]
172  
173 Gets or sets a canonicalization note for a specific field on the action.
174  
175 =cut
176  
177 0 ▶sub field_canonicalization_note {
Subroutine field_canonicalization_note: not called CC=2 SCAR=17.9
Pod documented
178 0 my $self = shift;
179 0 my $field = shift;
180  
181 0 ▶ $self->{field_canonicalization_notes}{ $field } = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
182 0 return $self->{field_canonicalization_notes}{ $field };
183 }
184  
185 =head2 field_canonicalization_notes
186  
187 Returns a hash which maps L<argument|Jifty::Manual::Glossary/argument>
188 name to canonicalization notes.
189  
190 =cut
191  
192 0 ▶sub field_canonicalization_notes {
Subroutine field_canonicalization_notes: not called CC=2 SCAR=17.9
Pod documented
193 0 my $self = shift;
194 0 ▶ return %{$self->{field_canonicalization_notes} || {}};
Condition0%0/2
$self->{'field_canonicalization_notes'} || {}
l!l
00
MC/DC0%0/1
$self->{'field_canonicalization_notes'} || {}
$self->{'field_canonicalization_notes'}
Truth table0%0/2
A
A= $self->{'field_canonicalization_notes'}
Aresult
11
00
195 }
196  
197 =head2 content [KEY [, VALUE]]
198  
199 Gets or sets the content C<KEY>. This is used when actions need to
200 return values. If not C<KEY> is passed, it returns an anonymous hash
201 of all of the C<KEY> and C<VALUE> pairs.
202  
203 =cut
204  
205 0 ▶sub content {
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
206 0 my $self = shift;
207  
208 0 ▶ return $self->_content unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
209  
210 0 my $key = shift;
211 0 ▶ $self->_content->{$key} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
212 0 return $self->_content->{$key};
213 }
214  
215 =head2 as_hash
216  
217 This returns the results as a hash to be given directly to the end user
218 (usually via REST or webservices). The difference between
219 C<< $result->as_hash >> and C<%$result> is that the latter will expand
220 everything as deeply as possible. The former won't inflate C<refers_to>
221 columns, among other things.
222  
223 =cut
224  
225 0 ▶sub as_hash {
Subroutine as_hash: not called CC=5 SCAR=34.0
Pod documented
226 0 my $self = shift;
227  
228 0 my $out = {
229 success => $self->success,
230 failure => $self->failure,
231 action_class => $self->action_class,
232 message => $self->message,
233 error => $self->error,
234 field_errors => { $self->field_errors },
235 field_warnings => { $self->field_warnings },
236 content => $self->_recurse_object_to_data($self->content),
237 };
238  
239 0 for (keys %{$out->{field_errors}}) {
240 0 ▶ delete $out->{field_errors}->{$_} unless $out->{field_errors}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $out->{"field_errors"}{$_} 0 0 0
241 }
242 0 for (keys %{$out->{field_warnings}}) {
243 0 ▶ delete $out->{field_warnings}->{$_} unless $out->{field_warnings}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $out->{"field_warnings"}{$_} 0 0 0
244 }
245  
246 0 return $out;
247 }
248  
249 0 ▶sub _recurse_object_to_data {
Subroutine _recurse_object_to_data: not called CC=5 SCAR=34.0
250 0 my $self = shift;
251 0 my $o = shift;
252  
253 0 ▶ return $o if !ref($o);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o 0 0 0
254  
255 0 ▶ if (ref($o) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $o eq 'ARRAY') { } 0 0 0
elsif (ref $o eq 'HASH') { } 0 0 0
256 0 return [ map { $self->_recurse_object_to_data($_) } @$o ];
257 }
258 elsif (ref($o) eq 'HASH') {
259 0 my %h;
260 0 $h{$_} = $self->_recurse_object_to_data($o->{$_}) for keys %$o;
261 0 return \%h;
262 }
263  
264 0 return $self->_object_to_data($o);
265 }
266  
267 0 ▶sub _object_to_data {
Subroutine _object_to_data: not called CC=2 SCAR=17.9
268 0 my $self = shift;
269 0 my $o = shift;
270  
271 0 ▶ if ($o->can('jifty_serialize_format')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->can("jifty_serialize_format")) 0 0 0
272 0 return $o->jifty_serialize_format($self);
273 }
274  
275 # As the last resort, return the object itself and expect the
276 # $accept-specific renderer to format the object as e.g. YAML or JSON data.
277 0 return $o;
278 }
279  
280 1 1;
« Jifty/Plugin.pm ↑ Summary Jifty/Web/Form/Field/Collection.pm »