blib/lib/Venus/Enum.pm

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