QBit/Validator/Type.pm untested

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