File Coverage

blib/lib/Class/Enumeration.pm
Criterion Covered Total %
statement 39 39 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 15 15 100.0
pod 0 6 0.0
total 65 71 91.5


line stmt bran cond sub pod time code
1             # Prefer numeric version for backwards compatibility
2 11     11   357254 BEGIN { require 5.010001 }; ## no critic ( RequireUseStrict, RequireUseWarnings )
3 11     11   96 use strict;
  11         36  
  11         287  
4 11     11   70 use warnings;
  11         28  
  11         1116  
5              
6             package Class::Enumeration;
7              
8             $Class::Enumeration::VERSION = 'v1.3.1';
9              
10             use overload
11             '""' => 'to_string',
12             '==' => '_is_identical_to',
13 1     1   252 '!=' => sub { !&_is_identical_to }, ## no critic ( ProhibitAmpersandSigils )
14 11     11   5814 fallback => 0;
  11         17782  
  11         101  
15              
16 11     11   1254 use Carp ();
  11         19  
  11         235  
17 11     11   49 use Scalar::Util ();
  11         17  
  11         4473  
18              
19             # $self == enum object
20             # $class == enum class
21              
22             sub name {
23 292     292 0 8595 my ( $self ) = @_;
24              
25             $self->{ name }
26 292         3653 }
27              
28             sub ordinal {
29 305     305 0 519 my ( $self ) = @_;
30              
31             $self->{ ordinal }
32 305         1501 }
33              
34             sub value_of {
35 57     57 0 51025 my ( $class, $name ) = @_;
36              
37 57         234 my ( $self ) = grep { $_->name eq $name } $class->values;
  167         414  
38 57 100       1839 defined $self ? $self : Carp::croak "No enum object defined for name '$name', stopped"
39             }
40              
41             sub values { ## no critic ( ProhibitBuiltinHomonyms )
42 1     1 0 274533 Carp::croak "'values()' method not implemented by child class, stopped"
43             }
44              
45             sub names {
46 8     8 0 3699 my ( $class ) = @_;
47              
48 8         46 map { $_->name } $class->values
  22         167  
49             }
50              
51             sub to_string {
52 19     19 0 64 my ( $self ) = @_;
53              
54 19         57 $self->name
55             }
56              
57             sub _new { ## no critic ( ProhibitUnusedPrivateSubroutines )
58 36     36   335608 my ( $class, $ordinal, $name, $attributes ) = @_;
59              
60 36 100       204 Carp::croak 'The enum object name cannot be empty, stopped'
61             if $name eq '';
62 35 100       145 $attributes = {} unless defined $attributes;
63             # Will raise a FATAL warning ("Not a HASH reference at ...") if $attribute is
64             # not a HASH reference
65 35         105 for ( keys %$attributes ) {
66 15 100 100     348 Carp::croak "Overriding the implicit '$_' enum object attribute is forbidden, stopped"
67             if $_ eq 'ordinal' or $_ eq 'name';
68             }
69              
70 32         255 bless { ordinal => $ordinal, name => $name, %$attributes }, $class
71             }
72              
73             sub _is_identical_to {
74 39     39   13604 my ( $self, $object, $swapFlag ) = @_;
75              
76 39         242 Scalar::Util::refaddr $self == Scalar::Util::refaddr $object;
77             }
78              
79             1