Validator/Type.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 92 CC 10 SCAR 43.7
CC
10
Cov
0%
check_options
110.0
get_all_options_name
2.0
merge_templates
2.0
CRAP 79.1
1 package QBit::Validator::Type;
2 $QBit::Validator::Type::VERSION = '0.009';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 __PACKAGE__->abstract_methods(qw(_get_options _get_options_name));
8  
9 sub check_options {
10 my ($self, $qv, $data, $template, $already_check, @path_field) = @_;
11  
12 return FALSE if $qv->has_error(\@path_field);
13  
14 if ($template->{'skip'}) {
15 $qv->_add_ok(\@path_field);
16  
17 return TRUE;
18 }
19  
20 my @options =
21 map {$_->{'name'}} grep {exists($template->{$_->{'name'}}) || $_->{'required'}} @{$self->_get_options()};
22  
23 foreach my $option (@options) {
24 if ($self->can($option)) {
25 last unless $self->$option($qv, $data, $template, $option, @path_field);
26 } else {
27 throw Exception::Validator gettext('Option "%s" don\'t have check sub', $option);
28 }
29 }
30  
31 return FALSE if $qv->has_error(\@path_field);
32  
33 if (exists($template->{'check'}) && !$already_check) {
34 $already_check = TRUE;
35  
36 throw Exception::Validator gettext('Option "check" must be code')
37 if !defined($template->{'check'}) || ref($template->{'check'}) ne 'CODE';
38  
39 next if !defined($data) && $template->{'optional'};
40  
41 my $error;
42 my $error_msg;
43 try {
44 $template->{'check'}($qv, $data, $template, @path_field);
45 }
46 catch Exception::Validator catch FF with {
47 $error = TRUE;
48 $error_msg = shift->message;
49 }
50 catch {
51 $error = TRUE;
52 $error_msg = gettext('Internal error');
53 };
54  
55 if ($error) {
56 $qv->_add_error($template, $error_msg, \@path_field, check_error => TRUE);
57  
58 return FALSE;
59 }
60 }
61  
62 $qv->_add_ok(\@path_field);
63  
64 return TRUE;
65 }
66  
67 sub get_all_options_name {
68 my ($self) = @_;
69  
70 return qw(skip type check msg), $self->_get_options_name();
71 }
72  
73 sub merge_templates {
74 my ($self, $template, $template2) = @_;
75  
76 return {
77 type => $template2->{'type'},
78 (
79 map {$_ => $template2->{$_}}
80 grep {!exists($template->{$_}) && $_ ne 'msg'} keys(%$template2)
81 ),
82 map {$_ => $template->{$_}} grep {$_ ne 'type' && $_ ne 'check'} keys(%$template)
83 };
84 }
85  
86 TRUE;