Enumeration/Builder.pm

filter: Statementstmt 100% 118 / 118 Branchbran 100% 28 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod n/a totaltotal 100% 173 / 173 CC 20 SCAR 19.5
CC
20
Cov
100%
import
17.0
_is_equal
4.0
BEGIN
1.0
CRAP 7.0
↑ Summary MY.pm »
1 # Prefer numeric version for backwards compatibility
2 8 ▶BEGIN { require 5.010001 }; ## no critic ( RequireUseStrict, RequireUseWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #<<<
8 package Class::Enumeration::Builder;
9 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 our $VERSION = 'v1.3.2';
11 }
12 #>>>
13  
14 8 ▶use subs qw( _create_enum_object _is_equal );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Sub::Util qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use Class::Enumeration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 13 ▶sub import {
Subroutine import: called CC=17 SCAR=28.3
22 13 shift;
23  
24 # TODO: Some options are relevant when import() is called at compile time;
25 # others if import() is called at runtime.
26 # If ( caller( 1 ) )[ 3 ] matches .*::BEGIN import() is called at compile
27 # time.
28 13 ▶ my $options = ref $_[ 0 ] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 10 3 13
29  
30 # $class == enum class
31 13 ▶ my $class = exists $options->{ class } ? delete $options->{ class } : caller;
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'class'} ? : 3 10 13
32 carp( "Enum class '$class' already built, warned" ), return $class ## no critic ( ProhibitCommaSeparatedStatements )
33 8 ▶ if do { no strict 'refs'; defined &{ "$class\::values" } }; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if do { no strict 'refs'; defined &{"${class}::values";} } 1 12 13
34  
35 # Now start building the enum class
36 {
37 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
38 12 push @{ "$class\::ISA" }, 'Class::Enumeration'
39 }
40  
41 12 my @values;
42 12 ▶ my $counter = exists $options->{ counter } ? delete $options->{ counter } : sub { state $i = 0; $i++ };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'counter'} ? : 2 10 12
43 12 ▶ my $prefix = exists $options->{ prefix } ? delete $options->{ prefix } : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'prefix'} ? : 1 11 12
44 # Check if custom attributes were provided
45 12 ▶ if ( ref $_[ 1 ] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 5 7 12
46 5 my ( $reference_name, $reference_attributes ) = @_[ 0 .. 1 ];
47 # Build list (@values) of enum objects
48 5 ▶ while ( my ( $name, $attributes ) = splice @_, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 12 3 15
49 12 ▶ croak "'$reference_name' enum and '$name' enum have different custom attributes, stopped"
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_equal($reference_attributes, $attributes) 2 10 12
50 unless _is_equal $reference_attributes, $attributes;
51 10 push @values, _create_enum_object $class, $counter, $prefix, $name, $attributes
52 }
53 # Build getters for custom attributes
54 3 for my $getter ( keys %$reference_attributes ) {
55 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶ *{ "$class\::$getter" } = set_subname "$class\::$getter" => sub { my ( $self ) = @_; $self->{ $getter } }
Subroutine foo: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
57 3 }
58 } else {
59 # Build list (@values) of enum objects
60 7 foreach my $name ( @_ ) {
61 20 push @values, _create_enum_object $class, $counter, $prefix, $name;
62 }
63 }
64  
65 {
66 10 {
67 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
68 # Inject list of enum objects
69 10 ▶ *{ "$class\::values" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 66 sort { $a->ordinal <=> $b->ordinal } @values
71 }
72 10 }
73 # Optionally build enum constants and set @EXPORT_OK and %EXPORT_TAGS
74 10 ▶ if ( delete $options->{ export } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $options->{"export"}) 4 6 10
75 4 my @names;
76 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
77 4 for my $self ( @values ) {
78 14 push @names, my $name = $self->name;
79 # export => 1 extends the API of the enum class itself
80 # Check t/LockFlag.t/class_api_ok test assertion
81 14 ▶ *{ "$class\::$name" } = sub () { $self }
Subroutine __ANON__: called CC=1 SCAR=0.0
82 14 }
83 4 *{ "$class\::EXPORT_OK" } = \@names;
84 4 *{ "$class\::EXPORT_TAGS" } = { all => \@names };
85 }
86 # Optionally build enum object predicate methods
87 10 ▶ if ( delete $options->{ predicate } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $options->{"predicate"}) 1 9 10
88 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 for my $self ( @values ) {
90 2 my $name = $self->name;
91 2 ▶ *{ "$class\::is_$name" } = sub { $_[ 0 ] == $self }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 2 }
93 }
94 10 ▶ if ( delete $options->{ to_json } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $options->{"to_json"}) 1 9 10
95 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶ *{ "$class\::TO_JSON" } = sub { $_[ 0 ]->name }
Subroutine __ANON__: called CC=1 SCAR=0.0
97 1 }
98 }
99  
100 10 ▶ croak "Unknown options '${ \( join( q/', '/, keys %$options ) ) }' detected, stopped"
Branch: 1 branch
BranchTrueFalseTotal
if %$options 1 9 10
101 if %$options;
102  
103 9 $class
104 }
105  
106 30 ▶sub _create_enum_object ( $$$$;$ ) {
Subroutine _create_enum_object: called CC=1 SCAR=0.0
107 30 my ( $class, $counter, $prefix, $name, $attributes ) = @_;
108  
109 # Put each enum object in its own (dedicated) child class of the parent
110 # enum class
111 30 my $child_class = "$class\::" . $prefix . $name;
112 {
113 8 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
114 30 push @{ "$child_class\::ISA" }, $class
115 }
116  
117 30 $child_class->_new( $counter->(), $prefix . $name, $attributes )
118 }
119  
120 # Compare 2 sets of hash keys
121 12 ▶sub _is_equal ( $$ ) {
Subroutine _is_equal: called CC=4 SCAR=13.9
122 12 my ( $reference_attributes, $attributes ) = @_;
123  
124 12 my @reference_attributes = keys %$reference_attributes;
125 12 ▶ return unless @reference_attributes == keys %$attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless @reference_attributes == keys %$attributes 1 11 12
126 11 for ( @reference_attributes ) {
127 10 ▶ return unless exists $attributes->{ $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attributes->{$_} 1 9 10
128 }
129 1
130 10 }
131  
132 8 1
↑ Summary MY.pm »