blib/lib/Venus/Coercion.pm

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