Custom/Result.pm

filter: Statementstmt 87.2% 82 / 94 Branchbran 87.5% 21 / 24 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 83.3% 20 / 24 Podpod 61.1% 11 / 18 totaltotal 83.7% 144 / 172 CC 19 SCAR 9.0
CC
19
Cov
87%
add_error_info
6.0
message
4.0
is_valid
4.0
CRAP 2.5
« Custom/Constraints.pm ↑ Summary Custom/FilterFunction.pm »
1 # Version 0 module
2 package Validator::Custom::Result;
3 5 ▶use Object::Simple -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Attrbutes
8 120 ▶has data => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
9 0 ▶has raw_data => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 172 ▶has missing_params => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 117 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=13.9
Pod documented
13 117 my ($self, $name) = @_;
14
15 117 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 116 1 117
16 116 ▶ return exists $self->{_error_infos}->{$name} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_error_infos'}{$name} ? : 67 49 116
17 }
18 else {
19 1 ▶ return !(keys %{$self->{_error_infos}}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!keys(%{$$self{'_error_infos'};}) ? : 0 1 1
20 }
21 }
22  
23 63 ▶sub message {
Subroutine message: called CC=4 SCAR=13.9
Pod documented
24 63 my ($self, $name) = @_;
25
26 # Parameter name not specified
27 63 ▶ croak 'Parameter name must be specified'
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 62 63
28 unless $name;
29
30 return $self->{_error_infos}->{$name}{message}
31 62 ▶ || $self->{_default_messages}{$name}
Condition80%4/5
$self->{'_error_infos'}{$name}{'message'} || $self->{'_default_messages'}{$name} || 'Error message not specified'
l!l
575
$self->{"_error_infos"}{$name}{"message"} or $self->{"_default_messages"}{$name}
l!l&&r!l&&!r
5705
MC/DC50%1/2
$self->{'_error_infos'}{$name}{'message'} || $self->{'_default_messages'}{$name} || 'Error message not specified'
$self->{"_error_infos"}{$name}{"message"} $self->{"_default_messages"}{$name}
Truth table66%2/3
A or B
A= $self->{"_error_infos"}{$name}{"message"}B= $self->{"_default_messages"}{$name}
ABresult
1X1
011
000
32 || 'Error message not specified';
33 }
34  
35 19 ▶sub messages {
Subroutine messages: called CC=2 SCAR=6.9
Pod documented
36 19 my $self = shift;
37  
38 # Error messages
39 19 my @messages;
40 19 my $error_infos = $self->{_error_infos};
41 19 my @keys = sort { $error_infos->{$a}{position} <=>
42 14 $error_infos->{$b}{position} } keys %$error_infos;
43 19 foreach my $name (@keys) {
44 29 my $message = $self->message($name);
45 29 push @messages, $message;
46 }
47
48 19 return \@messages;
49 }
50  
51 13 ▶sub messages_to_hash {
Subroutine messages_to_hash: called CC=2 SCAR=6.9
Pod documented
52 13 my $self = shift;
53  
54 # Error messages
55 13 my $messages = {};
56 13 foreach my $name (keys %{$self->{_error_infos}}) {
57 27 $messages->{$name} = $self->message($name);
58 }
59
60 13 return $messages;
61 }
62  
63 46 ▶sub is_ok {
Subroutine is_ok: called CC=3 SCAR=11.0
Pod documented
64 46 my $self = shift;
65
66 # Is ok?
67 46 ▶ return !$self->has_invalid && !$self->has_missing ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!($self->has_invalid || $self->has_missing) ? : 24 22 46
Condition100%3/3
$self->has_invalid || $self->has_missing
l!l&&r!l&&!r
21124
MC/DC100%2/2
$self->has_invalid || $self->has_missing
$self->has_invalid $self->has_missing
Truth table100%3/3
A || B
A= $self->has_invalidB= $self->has_missing
ABresult
1X1
011
000
68 }
69  
70 2 ▶sub to_hash {
Subroutine to_hash: called CC=1 SCAR=0.0
Pod documented
71 2 my $self = shift;
72
73 # Result
74 2 my $result = {};
75 2 $result->{ok} = $self->is_ok;
76 2 $result->{invalid} = $self->has_invalid;
77 2 $result->{missing} = $self->has_missing;
78 2 $result->{missing_params} = $self->missing_params;
79 2 $result->{messages} = $self->messages_to_hash;
80
81 2 return $result;
82 }
83  
84 42 ▶sub invalid_rule_keys {
Subroutine invalid_rule_keys: called CC=1 SCAR=0.0
Pod documented
85 42 my $self = shift;
86
87 # Invalid rule keys
88 42 my $error_infos = $self->{_error_infos};
89 42 my @invalid_rule_keys = sort { $error_infos->{$a}{position} <=>
90 35 $error_infos->{$b}{position} } keys %$error_infos;
91
92 42 return \@invalid_rule_keys;
93 }
94  
95 30 ▶sub has_missing { @{shift->missing_params} ? 1 : 0 }
Subroutine has_missing: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{(shift())->missing_params;} ? : 5 25 30
96  
97 58 ▶sub has_invalid {
Subroutine has_invalid: called CC=2 SCAR=6.9
Pod documented
98 58 my $self = shift;
99
100 # Has invalid parameter?
101 58 ▶ return keys %{$self->{_error_infos}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$self{'_error_infos'};} ? : 32 26 58
102 }
103  
104 3 ▶sub loose_data {
Subroutine loose_data: called CC=1 SCAR=0.0
Pod documented
105 3 my $self = shift;
106 3 return {%{$self->raw_data}, %{$self->data}};
107 }
108  
109 6 ▶sub invalid_params {
Subroutine invalid_params: called CC=3 SCAR=11.0
Pod documented
110 6 my $self = shift;
111
112 # Invalid parameter names
113 6 my @invalid_params;
114 6 foreach my $name (@{$self->invalid_rule_keys}) {
115 7 my $param = $self->{_error_infos}->{$name}{original_key};
116 7 ▶ $param = [$param] unless ref $param eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $param eq "ARRAY" 6 1 7
117 7 push @invalid_params, @$param;
118 }
119
120 6 return \@invalid_params;
121 }
122  
123  
124 # DEPRECATED!
125 4 ▶sub error_reason {
Subroutine error_reason: called CC=2 SCAR=6.9
Pod undocumented
126  
127 4 warn "Validator::Custom::Result error_reason is DEPRECATED!.";
128  
129 4 my ($self, $name) = @_;
130
131 # Parameter name not specified
132 4 ▶ croak 'Parameter name must be specified'
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 3 4
133 unless $name;
134
135 # Error reason
136 3 return $self->{_error_infos}->{$name}{reason};
137 }
138  
139 # DEPRECATED!
140 0 ▶has error_infos => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
141 # DEPRECATED!
142 0 ▶sub add_error_info {
Subroutine add_error_info: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my $self = shift;
144 0 warn "add_error_info method is DEPRECATED!";
145 # Merge
146 0 ▶ my $error_infos = ref $_[0] eq 'HASH' ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
147 0 $self->error_infos({%{$self->error_infos}, %$error_infos});
148 0 return $self;
149 }
150 # DEPRECATED!
151 3 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
152 3 warn "error_info method is DEPRECATED!";
153 3 shift->message(@_)
154 }
155 # DEPRECATED!
156 14 ▶sub errors {
Subroutine errors: called CC=2 SCAR=6.9
Pod undocumented
157 14 warn "errors method is DEPRECATED!";
158 return wantarray
159 14 ▶ ? @{shift->messages(@_)}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 6 14
160 : shift->messages(@_);
161 }
162 # DEPRECATED!
163 2 ▶sub errors_to_hash {
Subroutine errors_to_hash: called CC=1 SCAR=0.0
Pod undocumented
164 2 warn "errors_to_hash method is DEPRECATED!";
165 2 shift->messages_to_hash(@_)
166 }
167 # DEPRECATED!
168 31 ▶sub invalid_keys {
Subroutine invalid_keys: called CC=2 SCAR=6.9
Pod undocumented
169 31 warn "invalid_keys method is DEPRECATED!";
170 return wantarray
171 31 ▶ ? @{shift->invalid_rule_keys(@_)}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 2 31
172 : shift->invalid_rule_keys(@_);
173 }
174 # DEPRECATED!
175 0 ▶sub remove_error_info {
Subroutine remove_error_info: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my ($self, $key) = @_;
177 0 warn "remove_error_info method is DEPRECATED!";
178 # Remove
179 0 delete $self->error_infos->{$key};
180 0 return $self;
181 }
182  
183 5 1;
184  
185 =head1 NAME
186  
187 Validator::Custom::Result - Result of validation
188  
189 =head1 SYNOPSYS
190
191 # Result
192 my $result = $vc->validate($data, $rule);
193  
194 # Safety data
195 my $safe_data = $result->data;
196  
197 # Chacke if the result is valid.
198 # (this means result have neither missing nor invalid parameter)
199 my $is_ok = $result->is_ok;
200  
201 # Check the existence of missing parameter
202 my $has_missing_param = $result->has_missing;
203
204 # Check if one parameter is valid
205 my $title_is_valid = $result->is_valid('title');
206  
207 # Missing parameters(this is original keys)
208 my $missing_params = $result->missing_params;
209
210 # Invalid parameter names(this is original keys)
211 my $invalid_params = $result->invalid_params;
212
213 # Invalid rule keys
214 my $invalid_rule_keys = $result->invalid_rule_keys;
215  
216 # A error message
217 my $message = $result->message('title');
218  
219 # Error messages
220 my $messages = $result->messages;
221  
222 # Error messages to hash ref
223 my $messages_hash = $result->message_to_hash;
224
225 # Result to hash
226 my $rhash = $result->to_hash;
227
228 # Raw data
229 my $raw_data = $result->raw_data;
230
231  
232 =head1 ATTRIBUTES
233  
234 =head2 data
235  
236 my $data = $result->data;
237 $result = $result->data($data);
238  
239 Get the data in the end state. L<Validator::Custom> has filtering ability
240 if you need.
241 The data passed to C<validate()> is converted to other data by filter.
242 You can get filtered data using C<data()>.
243  
244 =head2 missing_params
245  
246 my $missing_params = $result->missing_params;
247 $result = $result->missing_params($missing_params);
248  
249 You can get missing parameter names using C<missing_params()>.
250 In this example, return value is the following one.
251  
252 =head2 raw_data
253  
254 my $data = $result->raw_data;
255 $result = $result->raw_data($data);
256  
257 Raw data soon after data_filter is executed.
258  
259 =head1 METHODS
260  
261 L<Validator::Custom::Result> inherits all methods from L<Object::Simple>
262 and implements the following new ones.
263  
264 =head2 has_invalid
265  
266 my $has_invalid = $result->has_invalid;
267  
268 If at least one of parameter value is invalid,
269 C<has_invalid()> return true value.
270  
271 =head2 has_missing
272  
273 my $has_missing_param = $result->has_missing;
274  
275 If at least one of parameter names specified in the rule
276 is not found in the data,
277 C<has_missing()> return true value.
278  
279 =head2 invalid_params
280  
281 my $invalid_params = $result->invalid_params;
282  
283 Invalid raw data parameter names.
284  
285 =head2 invalid_rule_keys
286  
287 my $invalid_rule_keys = $result->invalid_rule_keys;
288  
289 Invalid rule keys
290  
291 =head2 is_ok
292  
293 my $is_ok = $result->is_ok;
294  
295 If you check the data is completely valid, use C<is_ok()>.
296 C<is_ok()> return true value
297 if invalid parameter values is not found and all parameter
298 names specified in the rule is found in the data.
299  
300 =head2 is_valid
301  
302 my $title_is_valid = $result->is_valid('title');
303  
304 Check if one parameter is valid.
305  
306 =head2 loose_data
307  
308 my $loose_data = $result->loose_data;
309  
310 Loose data, which is data merged C<raw_data> and C<data>
311  
312 # Loose data
313 {%{$self->raw_data}, %{$self->data}}
314  
315 =head2 message
316  
317 my $message = $result->message('title');
318  
319 Get a message corresponding to the parameter name which value is invalid.
320  
321 =head2 messages
322  
323 my $messages = $result->messages;
324  
325 Get messages corresponding to the parameter names which value is invalid.
326 Messages keep the order of parameter names of the rule.
327  
328 =head2 messages_to_hash
329  
330 my $messages = $result->messages_to_hash;
331  
332 You can get the pairs of invalid parameter name and message
333 using C<messages_to_hash()>.
334  
335 =head2 to_hash
336  
337 my $rhash = $result->to_hash;
338  
339 Convert result information to hash reference.
340 The following keys is set.
341  
342 {
343 ok => $result->is_ok,
344 missing => $result->has_missing,
345 invalid => $result->has_invalid,
346 missing_params => $result->missing_params,
347 messages => $result->messages_to_hash
348 }
349  
350 =cut
« Custom/Constraints.pm ↑ Summary Custom/FilterFunction.pm »