blib/lib/Venus/Enum.pm

filter: Statementstmt 97.9% 94 / 96 Branchbran 75.0% 27 / 36 Conditioncond 52.9% 9 / 17 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 2 totaltotal 84.1% 154 / 183 CC 26 SCAR 11.5
CC
26
Cov
87%
__ANON__
6.0
__set
5.8
build_args
5.1
CRAP 3.2
« blib/lib/Venus/What.pm ↑ Summary blib/lib/Venus/Check.pm »
1 package Venus::Enum;
2  
3 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 2 ▶use Venus::Class 'base';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 2 base 'Venus::Sealed';
15  
16 # OVERLOADS
17  
18 2 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.1
19 13 ▶ '""' => sub{$_[0]->value // ''},
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->value // ""
l!l
67
MC/DC100%1/1
$_[0]->value // ""
$_[0]->value
Truth table100%2/2
A
A= $_[0]->value
Aresult
11
00
20 0 ▶ '~~' => sub{$_[0]->value // ''},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->value // ""
l!l
00
MC/DC0%0/1
$_[0]->value // ""
$_[0]->value
Truth table0%0/2
A
A= $_[0]->value
Aresult
11
00
21 2 ▶ 'eq' => sub{($_[0]->value // '') eq "$_[1]"},
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$_[0]->value // ""
l!l
02
MC/DC0%0/1
$_[0]->value // ""
$_[0]->value
Truth table50%1/2
A
A= $_[0]->value
Aresult
11
00
22 2 ▶ 'ne' => sub{($_[0]->value // '') ne "$_[1]"},
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->value // ""
l!l
11
MC/DC100%1/1
$_[0]->value // ""
$_[0]->value
Truth table100%2/2
A
A= $_[0]->value
Aresult
11
00
23 1 ▶ 'qr' => sub{qr/@{[quotemeta($_[0])]}/},
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 fallback => 1,
25 2 );
26  
27 # BUILDERS
28  
29 28 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
30 28 my ($self, $data) = @_;
31  
32 return {
33 28 value => $data,
34 };
35 }
36  
37 42 ▶sub build_args {
Subroutine build_args: called CC=5 SCAR=16.2
Pod undocumented
38 42 my ($self, $data) = @_;
39  
40 42 ▶ if (not(keys %$data == 1 && exists $data->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$data == 1 and exists $data->{"value"}) 2 40 42
Condition66%2/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
2040
MC/DC50%1/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1 exists $data->{"value"}
Truth table66%2/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
ABresult
0X0
100
111
41 2 $data = {value => $data};
42 }
43  
44 42 my $value = $data->{value};
45  
46 42 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 0 42 42
47 0 $value = {}
48 }
49  
50 42 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 29 13 42
51 29 $value = {map +(s/\W//gr, $_), @{$value}};
52 }
53 else {
54 13 $value = {map +(s/\W//gr, $value->{$_}), keys %{$value}};
55 }
56  
57 $data->{value} = {
58 names => $value,
59 42 codes => {reverse %{$value}},
60 };
61  
62 42 return $self->SUPER::build_args($data);
63 }
64  
65 # METHODS
66  
67 11 ▶sub __get {
Subroutine __get: called CC=3 SCAR=11.3
68 11 my ($self, $init, $data, $name) = @_;
69  
70 11 ▶ return undef if !$name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 11 11
71  
72 11 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
73  
74 11 my $enum = $class->new(value => $init->{value}->{names});
75  
76 11 $enum->{set} = 1;
77  
78 11 $enum->set($name);
79  
80 11 delete $enum->{set};
81  
82 11 return $enum;
83 }
84  
85 11 ▶sub __set {
Subroutine __set: called CC=5 SCAR=17.5
86 11 my ($self, $init, $data, $name) = @_;
87  
88 11 ▶ return undef if !$name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 11 11
89  
90 11 ▶ return $self if !exists $self->{set};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"set"} 0 11 11
91  
92 11 my $names = $init->{value}->{names};
93  
94 11 ▶ return $self if !exists $names->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $names->{$name} 0 11 11
95  
96 11 ▶ $data->{named} //= $name;
Condition33%1/3
$data->{"named"} //= $name
l!l&&r!l&&!r
0110
MC/DC0%0/2
$data->{"named"} //= $name
$data->{"named"} $name
Truth table33%1/3
A //= B
A= $data->{"named"}B= $name
ABresult
1X1
011
000
97  
98 11 return $self;
99 }
100  
101 2 ▶sub __has {
Subroutine __has: called CC=4 SCAR=14.0
102 2 my ($self, $init, $data, $match) = @_;
103  
104 2 ▶ return false if !$match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 2 2
105  
106 2 my $names = $init->{value}->{names};
107  
108 2 ▶ return true if $names->{$match};
Branch: 1 branch
BranchTrueFalseTotal
if $names->{$match} 1 1 2
109  
110 1 my $codes = $init->{value}->{codes};
111  
112 1 ▶ return true if $codes->{$match};
Branch: 1 branch
BranchTrueFalseTotal
if $codes->{$match} 0 1 1
113  
114 1 return false;
115 }
116  
117 2 ▶sub __is {
Subroutine __is: called CC=4 SCAR=14.0
118 2 my ($self, $init, $data, $match) = @_;
119  
120 2 ▶ return false if !$match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 2 2
121  
122 2 my $name = $self->name;
123  
124 2 ▶ return true if $name eq $match;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $match 1 1 2
125  
126 1 my $value = $self->value;
127  
128 1 ▶ return true if $value eq $match;
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $match 0 1 1
129  
130 1 return false;
131 }
132  
133 4 ▶sub __name {
Subroutine __name: called CC=1 SCAR=0.0
134 4 my ($self, $init, $data) = @_;
135  
136 4 return $data->{named};
137 }
138  
139 2 ▶sub __names {
Subroutine __names: called CC=2 SCAR=6.9
140 2 my ($self, $init, $data) = @_;
141  
142 2 my $names = $init->{value}->{names};
143  
144 2 my $list = [sort keys %{$names}];
145  
146 2 ▶ return wantarray ? (@{$list}) : $list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
147 }
148  
149 2 ▶sub __items {
Subroutine __items: called CC=2 SCAR=6.9
150 2 my ($self, $init, $data) = @_;
151  
152 2 my $names = $init->{value}->{names};
153  
154 2 my $list = [map [$_, $names->{$_}], $self->list];
155  
156 2 ▶ return wantarray ? (@{$list}) : $list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
157 }
158  
159 4 ▶sub __list {
Subroutine __list: called CC=2 SCAR=6.9
160 4 my ($self, $init, $data) = @_;
161  
162 4 my $codes = $init->{value}->{codes};
163  
164 4 my $list = [map $codes->{$_}, sort keys %{$codes}];
165  
166 4 ▶ return wantarray ? (@{$list}) : $list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
167 }
168  
169 22 ▶sub __value {
Subroutine __value: called CC=2 SCAR=6.9
170 22 my ($self, $init, $data) = @_;
171  
172 22 my $value = $data->{named};
173  
174 22 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 11 11 22
175  
176 11 return $init->{value}->{names}->{$value};
177 }
178  
179 2 ▶sub __values {
Subroutine __values: called CC=2 SCAR=6.9
180 2 my ($self, $init, $data) = @_;
181  
182 2 my $codes = $init->{value}->{codes};
183  
184 2 my $list = [sort keys %{$codes}];
185  
186 2 ▶ return wantarray ? (@{$list}) : $list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
187 }
188  
189 2 1;
190  
191  
192  
193 =head1 NAME
194  
195 Venus::Enum - Enum Class
196  
197 =cut
198  
199 =head1 ABSTRACT
200  
201 Enum Class for Perl 5
202  
203 =cut
204  
205 =head1 SYNOPSIS
206  
207 package main;
208  
209 use Venus::Enum;
210  
211 my $enum = Venus::Enum->new(['n', 's', 'e', 'w']);
212  
213 # my $north = $enum->get('n');
214  
215 # "n"
216  
217 =cut
218  
219 =head1 DESCRIPTION
220  
221 This package provides an interface for working with enumerations.
222  
223 =cut
224  
225 =head1 INHERITS
226  
227 This package inherits behaviors from:
228  
229 L<Venus::Sealed>
230  
231 =cut
232  
233 =head1 METHODS
234  
235 This package provides the following methods:
236  
237 =cut
238  
239 =head2 get
240  
241 get(string $name) (Venus::Enum)
242  
243 The get method returns a new object representing the enum member specified.
244  
245 I<Since C<3.55>>
246  
247 =over 4
248  
249 =item get example 1
250  
251 # given: synopsis
252  
253 package main;
254  
255 my $get = $enum->get('n');
256  
257 # bless(..., "Venus::Enum")
258  
259 # $get->value
260  
261 # "n"
262  
263 =back
264  
265 =over 4
266  
267 =item get example 2
268  
269 # given: synopsis
270  
271 package main;
272  
273 my $get = $enum->get('s');
274  
275 # bless(..., "Venus::Enum")
276  
277 # $get->value
278  
279 # "s"
280  
281 =back
282  
283 =cut
284  
285 =head2 has
286  
287 has(string $name) (boolean)
288  
289 The has method returns true if the member name or value exists in the enum,
290 otherwise returns false.
291  
292 I<Since C<3.55>>
293  
294 =over 4
295  
296 =item has example 1
297  
298 # given: synopsis
299  
300 package main;
301  
302 my $has = $enum->has('n');
303  
304 # true
305  
306 =back
307  
308 =over 4
309  
310 =item has example 2
311  
312 # given: synopsis
313  
314 package main;
315  
316 my $has = $enum->has('z');
317  
318 # false
319  
320 =back
321  
322 =cut
323  
324 =head2 is
325  
326 is(string $name) (boolean)
327  
328 The is method returns true if the member name or value specified matches the
329 member selected in the enum, otherwise returns false.
330  
331 I<Since C<3.55>>
332  
333 =over 4
334  
335 =item is example 1
336  
337 # given: synopsis
338  
339 package main;
340  
341 my $is = $enum->get('n')->is('n');
342  
343 # true
344  
345 =back
346  
347 =over 4
348  
349 =item is example 2
350  
351 # given: synopsis
352  
353 package main;
354  
355 my $is = $enum->get('s')->is('n');
356  
357 # false
358  
359 =back
360  
361 =cut
362  
363 =head2 items
364  
365 items() (tuple[string, string])
366  
367 The items method returns an arrayref of arrayrefs containing the name and value
368 pairs for the enumerations. Returns a list in list context.
369  
370 I<Since C<3.55>>
371  
372 =over 4
373  
374 =item items example 1
375  
376 # given: synopsis
377  
378 package main;
379  
380 my $items = $enum->items;
381  
382 # [["e", "e"], ["n", "n"], ["s", "s"], ["w", "w"]]
383  
384 =back
385  
386 =over 4
387  
388 =item items example 2
389  
390 # given: synopsis
391  
392 package main;
393  
394 my @items = $enum->items;
395  
396 # (["e", "e"], ["n", "n"], ["s", "s"], ["w", "w"])
397  
398 =back
399  
400 =cut
401  
402 =head2 list
403  
404 list() (within[arrayref, string])
405  
406 The list method returns an arrayref containing the values for the enumerations.
407 Returns a list in list context.
408  
409 I<Since C<3.55>>
410  
411 =over 4
412  
413 =item list example 1
414  
415 # given: synopsis
416  
417 package main;
418  
419 my $list = $enum->list;
420  
421 # ["e", "n", "s", "w"]
422  
423 =back
424  
425 =over 4
426  
427 =item list example 2
428  
429 # given: synopsis
430  
431 package main;
432  
433 my @list = $enum->list;
434  
435 # ("e", "n", "s", "w")
436  
437 =back
438  
439 =cut
440  
441 =head2 name
442  
443 name() (maybe[string])
444  
445 The name method returns the name of the member selected or returns undefined.
446  
447 I<Since C<3.55>>
448  
449 =over 4
450  
451 =item name example 1
452  
453 # given: synopsis
454  
455 package main;
456  
457 my $name = $enum->name;
458  
459 # undef
460  
461 =back
462  
463 =over 4
464  
465 =item name example 2
466  
467 # given: synopsis
468  
469 package main;
470  
471 my $n = $enum->get('n');
472  
473 my $name = $n->name;
474  
475 # "n"
476  
477 =back
478  
479 =cut
480  
481 =head2 names
482  
483 names() (within[arrayref, string])
484  
485 The names method returns an arrayref containing the names for the enumerations.
486 Returns a list in list context.
487  
488 I<Since C<3.55>>
489  
490 =over 4
491  
492 =item names example 1
493  
494 # given: synopsis
495  
496 package main;
497  
498 my $names = $enum->names;
499  
500 # ["e", "n", "s", "w"]
501  
502 =back
503  
504 =over 4
505  
506 =item names example 2
507  
508 # given: synopsis
509  
510 package main;
511  
512 my @names = $enum->names;
513  
514 # ("e", "n", "s", "w")
515  
516 =back
517  
518 =cut
519  
520 =head2 new
521  
522 new(any @args) (Venus::Enum)
523  
524 The new method constructs an instance of the package.
525  
526 I<Since C<4.15>>
527  
528 =over 4
529  
530 =item new example 1
531  
532 package main;
533  
534 use Venus::Enum;
535  
536 my $new = Venus::Enum->new;
537  
538 # bless(..., "Venus::Enum")
539  
540 =back
541  
542 =over 4
543  
544 =item new example 2
545  
546 package main;
547  
548 use Venus::Enum;
549  
550 my $new = Venus::Enum->new(['n', 's', 'e', 'w']);
551  
552 # bless(..., "Venus::Enum")
553  
554 =back
555  
556 =over 4
557  
558 =item new example 3
559  
560 package main;
561  
562 use Venus::Enum;
563  
564 my $new = Venus::Enum->new(value => ['n', 's', 'e', 'w']);
565  
566 # bless(..., "Venus::Enum")
567  
568 =back
569  
570 =cut
571  
572 =head2 value
573  
574 value() (maybe[string])
575  
576 The value method returns the value of the member selected or returns undefined.
577  
578 I<Since C<3.55>>
579  
580 =over 4
581  
582 =item value example 1
583  
584 # given: synopsis
585  
586 package main;
587  
588 my $value = $enum->value;
589  
590 # undef
591  
592 =back
593  
594 =over 4
595  
596 =item value example 2
597  
598 # given: synopsis
599  
600 package main;
601  
602 my $n = $enum->get('n');
603  
604 my $value = $n->value;
605  
606 # "n"
607  
608 =back
609  
610 =cut
611  
612 =head2 values
613  
614 values() (within[arrayref, string])
615  
616 The values method returns an arrayref containing the values for the
617 enumerations. Returns a list in list context.
618  
619 I<Since C<3.55>>
620  
621 =over 4
622  
623 =item values example 1
624  
625 # given: synopsis
626  
627 package main;
628  
629 my $values = $enum->values;
630  
631 # ["e", "n", "s", "w"]
632  
633 =back
634  
635 =over 4
636  
637 =item values example 2
638  
639 # given: synopsis
640  
641 package main;
642  
643 my @values = $enum->values;
644  
645 # ("e", "n", "s", "w")
646  
647 =back
648  
649 =cut
650  
651 =head1 OPERATORS
652  
653 This package overloads the following operators:
654  
655 =cut
656  
657 =over 4
658  
659 =item operation: C<("")>
660  
661 This package overloads the C<""> operator.
662  
663 B<example 1>
664  
665 # given: synopsis;
666  
667 my $result = "$enum";
668  
669 # ""
670  
671 B<example 2>
672  
673 # given: synopsis;
674  
675 my $n = $enum->get("n");
676  
677 my $result = "$n";
678  
679 # "n"
680  
681 =back
682  
683 =over 4
684  
685 =item operation: C<(eq)>
686  
687 This package overloads the C<eq> operator.
688  
689 B<example 1>
690  
691 # given: synopsis;
692  
693 my $result = $enum eq "";
694  
695 # 1
696  
697 B<example 2>
698  
699 # given: synopsis;
700  
701 my $s = $enum->get("s");
702  
703 my $result = $s eq "s";
704  
705 # 1
706  
707 =back
708  
709 =over 4
710  
711 =item operation: C<(ne)>
712  
713 This package overloads the C<ne> operator.
714  
715 B<example 1>
716  
717 # given: synopsis;
718  
719 my $result = $enum ne "";
720  
721 # 0
722  
723 B<example 2>
724  
725 # given: synopsis;
726  
727 my $n = $enum->get("n");
728  
729 my $result = $n ne "";
730  
731 # 1
732  
733 =back
734  
735 =over 4
736  
737 =item operation: C<(qr)>
738  
739 This package overloads the C<qr> operator.
740  
741 B<example 1>
742  
743 # given: synopsis;
744  
745 my $n = $enum->get('n');
746  
747 my $test = 'north' =~ qr/$n/;
748  
749 # 1
750  
751 =back
752  
753 =head1 AUTHORS
754  
755 Awncorp, C<awncorp@cpan.org>
756  
757 =cut
758  
759 =head1 LICENSE
760  
761 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
762  
763 This program is free software, you can redistribute it and/or modify it under
764 the terms of the Apache license version 2.0.
765  
766 =cut
« blib/lib/Venus/What.pm ↑ Summary blib/lib/Venus/Check.pm »