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