blib/lib/Salvation/TC/Meta/Type.pm

filter: Statementstmt 84.0% 74 / 88 Branchbran 60.7% 17 / 28 Conditioncond 47.6% 10 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 83.3% 20 / 24 Podpod 100% 16 / 16 totaltotal 72.8% 137 / 188 SCAR 35.4
CC
24
Cov
74%
new
23.8
coerce
5.2
__ANON__
2.1
CRAP 34.5
« blib/lib/Salvation/TC/Type/Date.pm ↑ Summary blib/lib/Salvation/TC/Meta/Type/Union.pm »
1 package Salvation::TC::Meta::Type;
2  
3 =head1 NAME
4  
5 Salvation::TC::Meta::Type - Класс для простых типов
6  
7 =cut
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use base 'Salvation::TC::Meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 METHODS
18  
19 =cut
20  
21 =head2 new()
22  
23 =cut
24  
25 sub new {
26  
27 178 ▶ my ( $proto, %args ) = @_;
Subroutine new: called CC=15 SCAR=31.7
Pod documented
28  
29 178 ▶ die( 'Type metaclass must have validator' ) unless( defined $args{ 'validator' } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"validator"} 0 178 178
30 178 ▶ die( 'Validator must be a CodeRef' ) if( ref( $args{ 'validator' } ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"validator"} ne "CODE" 0 178 178
31  
32 178 $args{ 'coercion_map' } = []; # Salvation::TC::Meta::Type => CodeRef
33  
34 178 ▶ if( exists $args{ 'parent' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"parent"}) 9 169 178
35  
36 9 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"parent"} and builtin::blessed($args{"parent"}) and $args{"parent"}->isa("Salvation::TC::Meta::Type")) 0 9 9
Condition33%2/6
defined $args{"parent"} and builtin::blessed($args{"parent"}) and $args{"parent"}->isa("Salvation::TC::Meta::Type")
!ll&&!rl&&r
009
defined $args{"parent"} and builtin::blessed($args{"parent"})
!ll&&!rl&&r
009
MC/DC0%0/3
defined $args{"parent"} and builtin::blessed($args{"parent"}) and $args{"parent"}->isa("Salvation::TC::Meta::Type")
defined $args{"parent"} builtin::blessed($args{"parent"}) $args{"parent"}->isa("Salvation::TC::Meta::Type")
Truth table25%1/4
A and B and C
A= defined $args{"parent"}B= builtin::blessed($args{"parent"})C= $args{"parent"}->isa("Salvation::TC::Meta::Type")
ABCresult
0XX0
10X0
1100
1111
37 defined $args{ 'parent' } && blessed $args{ 'parent' }
38 && $args{ 'parent' } -> isa( 'Salvation::TC::Meta::Type' )
39 ) {
40  
41 0 die( 'Parent type must be a Salvation::TC::Meta::Type' );
42 }
43  
44 9 my $self_validator = $args{ 'validator' };
45 9 my $parent_validator = $args{ 'parent' } -> validator();
46  
47 $args{ 'validator' } = sub {
48 27 ▶ $parent_validator -> ( $_[ 0 ] ) && $self_validator -> ( $_[ 0 ] )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
&$parent_validator($_[0]) and &$self_validator($_[0]) 22 0 22
49 9 };
50 }
51  
52 178 ▶ if( exists $args{ 'base' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"base"}) 74 104 178
53  
54 74 ▶ unless(
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"base"} and builtin::blessed($args{"base"}) and $args{"base"}->isa("Salvation::TC::Meta::Type")) 0 74 74
Condition33%2/6
defined $args{"base"} and builtin::blessed($args{"base"}) and $args{"base"}->isa("Salvation::TC::Meta::Type")
!ll&&!rl&&r
0074
defined $args{"base"} and builtin::blessed($args{"base"})
!ll&&!rl&&r
0074
MC/DC0%0/3
defined $args{"base"} and builtin::blessed($args{"base"}) and $args{"base"}->isa("Salvation::TC::Meta::Type")
defined $args{"base"} builtin::blessed($args{"base"}) $args{"base"}->isa("Salvation::TC::Meta::Type")
Truth table25%1/4
A and B and C
A= defined $args{"base"}B= builtin::blessed($args{"base"})C= $args{"base"}->isa("Salvation::TC::Meta::Type")
ABCresult
0XX0
10X0
1100
1111
55 defined $args{ 'base' } && blessed $args{ 'base' }
56 && $args{ 'base' } -> isa( 'Salvation::TC::Meta::Type' )
57 ) {
58  
59 0 die( 'Base type must be a Salvation::TC::Meta::Type' );
60 }
61 }
62  
63 178 foreach my $spec (
64 [ 'signed_type_generator', 'Signed type generator' ],
65 [ 'length_type_generator', 'Length type generator' ],
66 ) {
67 356 my ( $key, $name ) = @$spec;
68  
69 356 ▶ if( exists $args{ $key } && defined $args{ $key } && ( ref( $args{ $key } ) ne 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$key} and defined $args{$key} and ref $args{$key} ne "CODE") 0 356 356
Condition83%5/6
exists $args{$key} and defined $args{$key} and ref $args{$key} ne "CODE"
!ll&&!rl&&r
303530
exists $args{$key} and defined $args{$key}
!ll&&!rl&&r
15015353
MC/DC0%0/3
exists $args{$key} and defined $args{$key} and ref $args{$key} ne "CODE"
exists $args{$key} defined $args{$key} ref $args{$key} ne "CODE"
Truth table75%3/4
A and B and C
A= exists $args{$key}B= defined $args{$key}C= ref $args{$key} ne "CODE"
ABCresult
0XX0
10X0
1100
1111
70  
71 0 die( "${name} must be a CodeRef" );
72 }
73 }
74  
75 178 return $proto -> SUPER::new( %args );
76 }
77  
78 =head2 validator()
79  
80 =cut
81  
82 sub validator {
83  
84 391 ▶ my ( $self ) = @_;
Subroutine validator: called CC=1 SCAR=0.0
Pod documented
85  
86 391 return $self -> { 'validator' };
87 }
88  
89 =head2 check( Any $value )
90  
91 =cut
92  
93 sub check {
94  
95 335 ▶ my ( $self, $value ) = @_;
Subroutine check: called CC=1 SCAR=0.0
Pod documented
96  
97 335 return $self -> validator() -> ( $value );
98 }
99  
100 =head2 signature()
101  
102 =cut
103  
104 sub signature {
105  
106 1 ▶ my ( $self ) = @_;
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
107  
108 1 return $self -> { 'signature' };
109 }
110  
111 =head2 has_signature()
112  
113 =cut
114  
115 sub has_signature {
116  
117 24 ▶ my ( $self ) = @_;
Subroutine has_signature: called CC=1 SCAR=0.0
Pod documented
118  
119 24 return exists $self -> { 'signature' };
120 }
121  
122 =head2 coercion_map()
123  
124 =cut
125  
126 sub coercion_map {
127  
128 11 ▶ my ( $self ) = @_;
Subroutine coercion_map: called CC=1 SCAR=0.0
Pod documented
129  
130 11 return $self -> { 'coercion_map' };
131 }
132  
133 =head2 parent()
134  
135 =cut
136  
137 sub parent {
138  
139 0 ▶ my ( $self ) = @_;
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
140  
141 0 return $self -> { 'parent' };
142 }
143  
144 =head2 base()
145  
146 =cut
147  
148 sub base {
149  
150 0 ▶ my ( $self ) = @_;
Subroutine base: not called CC=1 SCAR=6.9
Pod documented
151  
152 0 return $self -> { 'base' };
153 }
154  
155 =head2 has_base()
156  
157 =cut
158  
159 sub has_base {
160  
161 0 ▶ my ( $self ) = @_;
Subroutine has_base: not called CC=1 SCAR=6.9
Pod documented
162  
163 0 return exists $self -> { 'base' };
164 }
165  
166 =head2 has_parent()
167  
168 =cut
169  
170 sub has_parent {
171  
172 0 ▶ my ( $self ) = @_;
Subroutine has_parent: not called CC=1 SCAR=6.9
Pod documented
173  
174 0 return exists $self -> { 'parent' };
175 }
176  
177 =head2 signed_type_generator()
178  
179 =cut
180  
181 sub signed_type_generator {
182  
183 42 ▶ my ( $self ) = @_;
Subroutine signed_type_generator: called CC=1 SCAR=0.0
Pod documented
184  
185 42 return $self -> { 'signed_type_generator' };
186 }
187  
188 =head2 length_type_generator()
189  
190 =cut
191  
192 sub length_type_generator {
193  
194 42 ▶ my ( $self ) = @_;
Subroutine length_type_generator: called CC=1 SCAR=0.0
Pod documented
195  
196 42 return $self -> { 'length_type_generator' };
197 }
198  
199 =head2 sign( ArrayRef $signature )
200  
201 Генерирует валидатор для текущего типа на основе подписи.
202  
203 =cut
204  
205 sub sign {
206  
207 8 ▶ my ( $self, $signature ) = @_;
Subroutine sign: called CC=2 SCAR=7.3
Pod documented
208  
209 8 my $signed_type_generator = $self -> signed_type_generator();
210  
211 8 ▶ unless( defined $signed_type_generator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $signed_type_generator) 0 8 8
212  
213 0 die( sprintf( 'Type %s cannot be signed', $self -> name() ) )
214 }
215  
216 8 my $signed_validator = $signed_type_generator -> ( $signature );
217  
218 return sub {
219  
220 16 ▶ $self -> check( $_[ 0 ] ) && $signed_validator -> ( $_[ 0 ] )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->check($_[0]) and &$signed_validator($_[0]) 16 0 16
221 8 };
222 }
223  
224 =head2 length_checker( Int $min, Maybe[Int] $max )
225  
226 Генерирует валидатор для текущего типа на основе спецификации длины.
227  
228 =cut
229  
230 sub length_checker {
231  
232 19 ▶ my ( $self, $min, $max ) = @_;
Subroutine length_checker: called CC=2 SCAR=7.3
Pod documented
233  
234 19 my $length_type_generator = $self -> length_type_generator();
235  
236 19 ▶ unless( defined $length_type_generator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length_type_generator) 0 19 19
237  
238 0 die( sprintf( 'Length of type %s could not be checked', $self -> name() ) );
239 }
240  
241 19 my $length_validator = $length_type_generator -> ( $min, $max );
242  
243 return sub {
244  
245 32 ▶ $self -> check( $_[ 0 ] ) && $length_validator -> ( $_[ 0 ] )
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->check($_[0]) and &$length_validator($_[0]) 32 0 32
246 19 };
247 }
248  
249 =head2 add_coercion( Salvation::TC::Meta::Type $from, CodeRef $how )
250  
251 =cut
252  
253 sub add_coercion {
254  
255 5 ▶ my ( $self, $from, $how ) = @_;
Subroutine add_coercion: called CC=1 SCAR=0.0
Pod documented
256  
257 5 push( @{ $self -> { 'coercion_map' } }, [ $from => $how ] );
258  
259 5 return;
260 }
261  
262 =head2 coerce( Any $value )
263  
264 =cut
265  
266 sub coerce {
267  
268 11 ▶ my ( $self, $value ) = @_;
Subroutine coerce: called CC=5 SCAR=16.5
Pod documented
269  
270 11 foreach my $rule ( @{ $self -> coercion_map() } ) {
271  
272 10 eval { $rule -> [ 0 ] -> check( $value ) };
273  
274 10 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 7 10
275  
276 3 ▶ if( blessed( $@ ) && $@ -> isa( 'Salvation::TC::Exception::WrongType' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa('Salvation::TC::Exception::WrongType')) { } 3 0 3
Condition33%1/3
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($@) and $@->isa("Salvation::TC::Exception::WrongType")
builtin::blessed($@) $@->isa("Salvation::TC::Exception::WrongType")
Truth table33%1/3
A and B
A= builtin::blessed($@)B= $@->isa("Salvation::TC::Exception::WrongType")
ABresult
0X0
100
111
277  
278 3 next;
279  
280 } else {
281  
282 0 die( $@ );
283 }
284 };
285  
286 7 local $_ = $value; # Ради соответствия API Moose
287  
288 7 $value = $rule -> [ 1 ] -> ();
289  
290 7 last;
291 }
292  
293 11 return $value; # Moose возвращает либо старое, либо приведённое значение
294 }
295  
296 4 1;
297  
298 __END__
« blib/lib/Salvation/TC/Type/Date.pm ↑ Summary blib/lib/Salvation/TC/Meta/Type/Union.pm »