blib/lib/Data/Validator/MultiManager.pm

filter: Statementstmt 93.6% 88 / 94 Branchbran 55.5% 10 / 18 Conditioncond 40.0% 4 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 91.6% 22 / 24 Podpod 100% 4 / 4 totaltotal 82.6% 129 / 156 CC 16 SCAR 10.0
CC
16
Cov
84%
_load_class
6.4
is_valid
6.0
add
4.1
CRAP 2.7
↑ Summary
1 package Data::Validator::MultiManager;
2 5 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = "0.01";
10  
11 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
12 4 my ($class, $validator) = @_;
13  
14 4 ▶ $validator ||= 'Data::Validator';
Condition100%2/2
$validator ||= "Data::Validator"
l!l
13
MC/DC100%1/1
$validator ||= "Data::Validator"
$validator
Truth table100%2/2
A
A= $validator
Aresult
11
00
15 4 _load_class($validator);
16  
17 4 bless {
18 validator_class => $validator,
19 priority => [],
20 validators => {},
21 common => {},
22 }, $class;
23 }
24  
25 4 ▶sub add {
Subroutine add: called CC=4 SCAR=14.1
Pod documented
26 4 my ($self, @args) = @_;
27 4 ▶ croak 'must be specified key-value pair' unless @args && scalar @args % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @args and scalar @args % 2 == 0 0 4 4
Condition33%1/3
@args and scalar @args % 2 == 0
!ll&&!rl&&r
004
MC/DC0%0/2
@args and scalar @args % 2 == 0
@args scalar @args % 2 == 0
Truth table33%1/3
A and B
A= @argsB= scalar @args % 2 == 0
ABresult
0X0
100
111
28  
29 4 ▶ while (my ($tag, $rule) = splice @args, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 8 4 12
30 8 my %merged_rule = (%{clone $self->{common}}, %$rule);
31 8 my $validator = $self->{validator_class}->new(%merged_rule);
32 8 $validator->with('NoThrow', 'NoRestricted');
33  
34 8 push @{$self->{priority}}, $tag;
35 8 $self->{validators}->{$tag} = $validator;
36 }
37 }
38  
39 1 ▶sub common {
Subroutine common: called CC=1 SCAR=0.0
Pod documented
40 1 my ($self, %rule) = @_;
41 1 $self->{common} = \%rule;
42 }
43  
44 14 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
45 14 my ($self, $param) = @_;
46 14 my $result = Data::Validator::MultiManager::Result->new($param, $self->{priority});
47  
48 14 for my $tag (@{$self->{priority}}) {
49 25 my $validator = $self->{validators}->{$tag};
50 25 my $args = $validator->validate($param);
51  
52 25 ▶ if (my $errors = $validator->clear_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $validator->clear_errors) { } 19 6 25
53 19 $result->set_errors($tag, $errors);
54 }
55 else {
56 6 $result->set_tag($tag);
57 6 $result->set_value($tag, $args);
58 6 return $result;
59 }
60 }
61 8 return $result;
62 }
63  
64 # copy from Plack::Util
65 4 ▶sub _load_class {
Subroutine _load_class: called CC=4 SCAR=18.6
66 4 my($class, $prefix) = @_;
67  
68 4 ▶ if ($prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 4 4
69 0 ▶ unless ($class =~ s/^\+// || $class =~ /^$prefix/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ s/^\+// or $class =~ /^$prefix/) 0 0 0
Condition0%0/3
$class =~ s/^\+// or $class =~ /^$prefix/
l!l&&r!l&&!r
000
MC/DC0%0/2
$class =~ s/^\+// or $class =~ /^$prefix/
$class =~ s/^\+// $class =~ /^$prefix/
Truth table0%0/3
A or B
A= $class =~ s/^\+//B= $class =~ /^$prefix/
ABresult
1X1
011
000
70 0 $class = "$prefix\::$class";
71 }
72 }
73  
74 4 my $file = $class;
75 4 $file =~ s!::!/!g;
76 4 require "$file.pm"; ## no critic
77  
78 4 return $class;
79 }
80  
81 package
82 Data::Validator::MultiManager::Result;
83  
84 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
85 14 my ($class, $original, $priority) = @_;
86  
87 14 my $self = bless {
88 priority => $priority,
89 errors => {},
90 tag => '',
91 values => {},
92 }, $class;
93 14 $self->set_original($original);
94 14 return $self;
95 }
96  
97 19 ▶sub set_errors {
Subroutine set_errors: called CC=1 SCAR=0.0
98 19 my ($self, $tag, $errors) = @_;
99 19 $self->{errors}->{$tag} = $errors;
100 }
101  
102 6 ▶sub set_value {
Subroutine set_value: called CC=1 SCAR=0.0
103 6 my ($self, $tag, $value) = @_;
104 6 $self->{values}->{$tag} = $value;
105 }
106  
107 14 ▶sub set_original {
Subroutine set_original: called CC=1 SCAR=0.0
108 14 my ($self, $value) = @_;
109 14 $self->{values}->{_original} = $value;
110 }
111  
112 6 ▶sub set_tag {
Subroutine set_tag: called CC=1 SCAR=0.0
113 6 my ($self, $tag) = @_;
114 6 $self->{tag} = $tag;
115 }
116  
117 0 ▶sub original {
Subroutine original: not called CC=1 SCAR=6.9
118 0 my $self = shift;
119 0 return $self->{values}->{_original};
120 }
121  
122 12 ▶sub valid {
Subroutine valid: called CC=1 SCAR=0.0
123 12 my $self = shift;
124 12 return $self->{tag};
125 }
126  
127 4 ▶sub invalid {
Subroutine invalid: called CC=1 SCAR=0.0
128 4 my $self = shift;
129 4 return $self->guess_error_tag_to_match;
130 }
131  
132 0 ▶sub is_valid {
Subroutine is_valid: not called CC=2 SCAR=17.9
133 0 my $self = shift;
134 0 ▶ return ($self->{tag})? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tag'} ? : 0 0 0
135 }
136  
137 3 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
138 3 my $self = shift;
139 3 return $self->{tag};
140 }
141  
142 3 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
143 3 my $self = shift;
144  
145 3 my $tag = $self->tag;
146 3 return $self->{values}->{$tag};
147 }
148  
149 4 ▶sub error {
Subroutine error: called CC=2 SCAR=7.0
150 4 my ($self, $tag) = @_;
151  
152 4 my $errors = $self->errors($tag);
153  
154 4 ▶ return undef unless $errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $errors 0 4 4
155 4 return $errors->[0];
156 }
157  
158 16 ▶sub errors {
Subroutine errors: called CC=3 SCAR=11.0
159 16 my ($self, $tag) = @_;
160  
161 16 ▶ unless ($tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag) 4 12 16
162 4 $tag = $self->guess_error_tag_to_match;
163 }
164 16 ▶ return $self->{errors}->{$tag} || [];
Condition50%1/2
$self->{'errors'}{$tag} || []
l!l
160
MC/DC0%0/1
$self->{'errors'}{$tag} || []
$self->{'errors'}{$tag}
Truth table50%1/2
A
A= $self->{'errors'}{$tag}
Aresult
11
00
165 }
166  
167 8 ▶sub guess_error_tag_to_match {
Subroutine guess_error_tag_to_match: called CC=3 SCAR=11.0
168 8 my ($self) = @_;
169  
170 8 my %diff;
171 8 for my $tag (reverse @{$self->{priority}}) {
172 16 ▶ if (my $errors = $self->{errors}->{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $self->{"errors"}{$tag}) 16 0 16
173 16 my $error_size = scalar @$errors;
174 16 $diff{$error_size} = $tag;
175 }
176 }
177 8 my $min = (sort keys %diff)[0];
178 8 return $diff{$min};
179 }
180  
181 5 1;
182 __END__
↑ Summary