blib/lib/Venus/Constraint.pm

filter: Statementstmt 94.6% 53 / 56 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 13 / 14 Podpod 77.7% 7 / 9 totaltotal 87.0% 87 / 100 CC 11 SCAR 10.5
CC
11
Cov
89%
build_arg
6.0
eval
5.0
accept
3.1
CRAP 2.8
« blib/lib/Venus/Path.pm ↑ Summary blib/lib/Venus/Unpack.pm »
1 package Venus::Constraint;
2  
3 12 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 12 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use Venus::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # INHERITS
15  
16 12 base 'Venus::Kind::Utility';
17  
18 # INTEGRATES
19  
20 12 with 'Venus::Role::Buildable';
21  
22 # ATTRIBUTES
23  
24 12 attr 'on_eval';
25  
26 # BUILDERS
27  
28 0 ▶sub build_arg {
Subroutine build_arg: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my ($self, $data) = @_;
30  
31 return {
32 0 ▶ on_eval => ref $data eq 'ARRAY' ? $data : [$data],
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 0 0 0
33 };
34 }
35  
36 240 ▶sub build_args {
Subroutine build_args: called CC=2 SCAR=7.1
Pod undocumented
37 240 my ($self, $data) = @_;
38  
39 240 ▶ $data->{on_eval} = [] if !$data->{on_eval};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"on_eval"} 240 0 240
40  
41 240 return $data;
42 }
43  
44 # METHODS
45  
46 14 ▶sub accept {
Subroutine accept: called CC=3 SCAR=11.2
Pod documented
47 14 my ($self, $name, @args) = @_;
48  
49 14 ▶ if (!$name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 14 14
50 0 return $self;
51 }
52 14 ▶ if ($self->check->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check->can($name)) { } 13 1 14
53 13 $self->check->accept($name, @args);
54 }
55 else {
56 1 $self->check->identity($name, @args);
57 }
58 14 return $self;
59 }
60  
61 530 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod documented
62 530 my ($self, @args) = @_;
63  
64 530 ▶ $self->{check} = $args[0] if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 222 308 530
65  
66 530 ▶ return $self->{check} ||= Venus::Check->new;
Condition66%2/3
$self->{"check"} ||= "Venus::Check"->new
l!l&&r!l&&!r
515150
MC/DC0%0/2
$self->{"check"} ||= "Venus::Check"->new
$self->{"check"} "Venus::Check"->new
Truth table66%2/3
A ||= B
A= $self->{"check"}B= "Venus::Check"->new
ABresult
1X1
011
000
67 }
68  
69 2 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
70 2 my ($self) = @_;
71  
72 2 @{$self->on_eval} = ();
73  
74 2 $self->check->clear;
75  
76 2 return $self;
77 }
78  
79 10 ▶sub ensure {
Subroutine ensure: called CC=1 SCAR=0.0
Pod documented
80 10 my ($self, @code) = @_;
81  
82 10 push @{$self->on_eval}, @code;
83  
84 10 return $self;
85 }
86  
87 276 ▶sub eval {
Subroutine eval: called CC=5 SCAR=16.1
Pod documented
88 276 my ($self, $data) = @_;
89  
90 276 my $result = false;
91  
92 276 ▶ if (!($result = $self->check->eval($data))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->check->eval($data)) 97 179 276
93 97 return $result;
94 }
95  
96 179 for my $callback (@{$self->on_eval}) {
97 16 local $_ = $data;
98 16 ▶ $result = $self->$callback($data) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->$callback($data) ? : 10 6 16
99 16 ▶ last if !$result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 6 10 16
100 }
101  
102 179 return $result;
103 }
104  
105 2 ▶sub evaler {
Subroutine evaler: called CC=1 SCAR=0.0
Pod documented
106 2 my ($self) = @_;
107  
108 2 return $self->defer('eval');
109 }
110  
111 266 ▶sub result {
Subroutine result: called CC=1 SCAR=0.0
Pod documented
112 266 my ($self, $data) = @_;
113  
114 266 return $self->eval($data);
115 }
116  
117 12 1;
118  
119  
120  
121 =head1 NAME
122  
123 Venus::Constraint - Constraint Class
124  
125 =cut
126  
127 =head1 ABSTRACT
128  
129 Constraint Class for Perl 5
130  
131 =cut
132  
133 =head1 SYNOPSIS
134  
135 package main;
136  
137 use Venus::Constraint;
138  
139 my $constraint = Venus::Constraint->new;
140  
141 # $constraint->accept('float');
142  
143 # $constraint->ensure(sub{$_ > 1});
144  
145 # $constraint->result(1.01);
146  
147 # true
148  
149 =cut
150  
151 =head1 DESCRIPTION
152  
153 This package provides a mechanism for evaluating type constraints on data.
154 Built-in type constraints are handled via L<Venus::Check>.
155  
156 =cut
157  
158 =head1 INHERITS
159  
160 This package inherits behaviors from:
161  
162 L<Venus::Kind::Utility>
163  
164 =cut
165  
166 =head1 INTEGRATES
167  
168 This package integrates behaviors from:
169  
170 L<Venus::Role::Buildable>
171  
172 =cut
173  
174 =head1 METHODS
175  
176 This package provides the following methods:
177  
178 =cut
179  
180 =head2 accept
181  
182 accept(string $name, any @args) (Venus::Constraint)
183  
184 The accept method registers a condition via L</check> based on the arguments
185 provided. The built-in types are defined as methods in L<Venus::Check>.
186  
187 I<Since C<3.55>>
188  
189 =over 4
190  
191 =item accept example 1
192  
193 # given: synopsis
194  
195 package main;
196  
197 $constraint = $constraint->accept('float');
198  
199 # bless(..., "Venus::Constraint")
200  
201 # $constraint->result;
202  
203 # false
204  
205 # $constraint->result(1.01);
206  
207 # true
208  
209 =back
210  
211 =over 4
212  
213 =item accept example 2
214  
215 # given: synopsis
216  
217 package main;
218  
219 $constraint = $constraint->accept('number');
220  
221 # bless(..., "Venus::Constraint")
222  
223 # $constraint->result(1.01);
224  
225 # false
226  
227 # $constraint->result(1_01);
228  
229 # true
230  
231 =back
232  
233 =over 4
234  
235 =item accept example 3
236  
237 # given: synopsis
238  
239 package Example1;
240  
241 sub new {
242 bless {};
243 }
244  
245 package Example2;
246  
247 sub new {
248 bless {};
249 }
250  
251 package main;
252  
253 $constraint = $constraint->accept('object');
254  
255 # bless(..., "Venus::Constraint")
256  
257 # $constraint->result;
258  
259 # false
260  
261 # $constraint->result(qr//);
262  
263 # false
264  
265 # $constraint->result(Example1->new);
266  
267 # true
268  
269 # $constraint->result(Example2->new);
270  
271 # true
272  
273 =back
274  
275 =over 4
276  
277 =item accept example 4
278  
279 # given: synopsis
280  
281 package Example1;
282  
283 sub new {
284 bless {};
285 }
286  
287 package Example2;
288  
289 sub new {
290 bless {};
291 }
292  
293 package main;
294  
295 $constraint = $constraint->accept('Example1');
296  
297 # bless(..., "Venus::Constraint")
298  
299 # $constraint->result;
300  
301 # false
302  
303 # $constraint->result(qr//);
304  
305 # false
306  
307 # $constraint->result(Example1->new);
308  
309 # true
310  
311 # $constraint->result(Example2->new);
312  
313 # false
314  
315 =back
316  
317 =cut
318  
319 =head2 check
320  
321 check(Venus::Check $data) (Venus::Check)
322  
323 The check method gets or sets the L<Venus::Check> object used for performing
324 runtime data type validation.
325  
326 I<Since C<3.55>>
327  
328 =over 4
329  
330 =item check example 1
331  
332 # given: synopsis
333  
334 package main;
335  
336 my $check = $constraint->check(Venus::Check->new);
337  
338 # bless(..., 'Venus::Check')
339  
340 =back
341  
342 =over 4
343  
344 =item check example 2
345  
346 # given: synopsis
347  
348 package main;
349  
350 $constraint->check(Venus::Check->new);
351  
352 my $check = $constraint->check;
353  
354 # bless(..., 'Venus::Check')
355  
356 =back
357  
358 =cut
359  
360 =head2 clear
361  
362 clear() (Venus::Constraint)
363  
364 The clear method resets the L</check> attributes and returns the invocant.
365  
366 I<Since C<3.55>>
367  
368 =over 4
369  
370 =item clear example 1
371  
372 # given: synopsis
373  
374 package main;
375  
376 $constraint->accept('string');
377  
378 $constraint = $constraint->clear;
379  
380 # bless(..., "Venus::Constraint")
381  
382 =back
383  
384 =cut
385  
386 =head2 ensure
387  
388 ensure(coderef $code) (Venus::Constraint)
389  
390 The ensure method registers a custom (not built-in) constraint condition and
391 returns the invocant.
392  
393 I<Since C<3.55>>
394  
395 =over 4
396  
397 =item ensure example 1
398  
399 # given: synopsis
400  
401 package main;
402  
403 $constraint->accept('number');
404  
405 my $ensure = $constraint->ensure(sub {
406 $_ >= 0
407 });
408  
409 # bless(.., "Venus::Constraint")
410  
411 =back
412  
413 =over 4
414  
415 =item ensure example 2
416  
417 # given: synopsis
418  
419 package main;
420  
421 $constraint->accept('number');
422  
423 my $ensure = $constraint->ensure(sub {
424 my ($source, $value) = @_;
425  
426 if ($value >= 0) {
427 return 1;
428 }
429 else {
430 return 0;
431 }
432 });
433  
434 # bless(..., "Venus::Constraint")
435  
436 =back
437  
438 =cut
439  
440 =head2 eval
441  
442 eval(any $data) (boolean)
443  
444 The eval method dispatches to the L</check> object as well as evaluating any
445 custom conditions, and returns true if all conditions pass, and false if any
446 condition fails.
447  
448 I<Since C<3.55>>
449  
450 =over 4
451  
452 =item eval example 1
453  
454 # given: synopsis
455  
456 package main;
457  
458 $constraint->accept('float');
459  
460 $constraint->ensure(sub{$_ >= 1});
461  
462 my $eval = $constraint->eval('1.00');
463  
464 # true
465  
466 =back
467  
468 =over 4
469  
470 =item eval example 2
471  
472 # given: synopsis
473  
474 package main;
475  
476 $constraint->accept('float');
477  
478 $constraint->ensure(sub{$_ >= 1});
479  
480 my $eval = $constraint->eval('0.99');
481  
482 # false
483  
484 =back
485  
486 =cut
487  
488 =head2 evaler
489  
490 evaler(any @args) (coderef)
491  
492 The evaler method returns a coderef which calls the L</eval> method with the
493 invocant when called.
494  
495 I<Since C<3.55>>
496  
497 =over 4
498  
499 =item evaler example 1
500  
501 # given: synopsis
502  
503 package main;
504  
505 my $evaler = $constraint->evaler;
506  
507 # sub{...}
508  
509 # my $result = $evaler->();
510  
511 # false
512  
513 =back
514  
515 =over 4
516  
517 =item evaler example 2
518  
519 # given: synopsis
520  
521 package main;
522  
523 my $evaler = $constraint->accept('any')->evaler;
524  
525 # sub{...}
526  
527 # my $result = $evaler->();
528  
529 # true
530  
531 =back
532  
533 =cut
534  
535 =head2 new
536  
537 new(any @args) (Venus::Constraint)
538  
539 The new method constructs an instance of the package.
540  
541 I<Since C<4.15>>
542  
543 =over 4
544  
545 =item new example 1
546  
547 package main;
548  
549 use Venus::Constraint;
550  
551 my $new = Venus::Constraint->new;
552  
553 # bless(..., "Venus::Constraint")
554  
555 =back
556  
557 =cut
558  
559 =head2 result
560  
561 result(any $data) (boolean)
562  
563 The result method dispatches to the L</eval> method and returns the result.
564  
565 I<Since C<3.55>>
566  
567 =over 4
568  
569 =item result example 1
570  
571 # given: synopsis
572  
573 package main;
574  
575 $constraint->accept('float');
576  
577 $constraint->ensure(sub{$_ >= 1});
578  
579 my $result = $constraint->result('1.00');
580  
581 # true
582  
583 =back
584  
585 =over 4
586  
587 =item result example 2
588  
589 # given: synopsis
590  
591 package main;
592  
593 $constraint->accept('float');
594  
595 $constraint->ensure(sub{$_ >= 1});
596  
597 my $result = $constraint->result('0.99');
598  
599 # false
600  
601 =back
602  
603 =cut
604  
605 =head1 AUTHORS
606  
607 Awncorp, C<awncorp@cpan.org>
608  
609 =cut
610  
611 =head1 LICENSE
612  
613 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
614  
615 This program is free software, you can redistribute it and/or modify it under
616 the terms of the Apache license version 2.0.
617  
618 =cut
« blib/lib/Venus/Path.pm ↑ Summary blib/lib/Venus/Unpack.pm »