| 1 | package Class::Type::Enum; | ||||||||||||||||||||
| 2 | # ABSTRACT: Build Enum-like classes | ||||||||||||||||||||
| 3 | 3 | $Class::Type::Enum::VERSION = '0.014'; | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use Class::Method::Modifiers qw(install_modifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use List::Util 1.33; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 3 | ▶ | use overload ( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 3 | '""' => 'stringify', | |||||||||||||||||||
| 17 | 'cmp' => 'cmp', | ||||||||||||||||||||
| 18 | '0+' => 'numify', | ||||||||||||||||||||
| 19 | fallback => 1, | ||||||||||||||||||||
| 20 | 3 | ); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 4 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=16.3
| |||||||||||||||||||||
| 25 | 4 | my ($class, %params) = @_; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # import is inherited, but we don't want to do all this to everything that | ||||||||||||||||||||
| 28 | # uses a subclass of Class::Type::Enum. | ||||||||||||||||||||
| 29 | 4 | ▶ | return unless $class eq __PACKAGE__; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | # If there's a use case for it, we can still allow extending CTE subclasses. | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | my $target = caller; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 3 | my %values; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | ▶ | if (ref $params{values} eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 37 | 3 | my $i = 0; | |||||||||||||||||||
| 38 | 3 | %values = map { $_ => $i++ } @{$params{values}}; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | elsif (ref $params{values} eq 'HASH') { | ||||||||||||||||||||
| 41 | 0 | %values = %{$params{values}}; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||
| 44 | 0 | croak "Enum values must be provided either as an array or hash ref."; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | ## the bits that are installed into the target class, plus @ISA | ||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 50 | 3 | push @{"${target}::ISA"}, $class; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | 3 | ▶ | install_modifier $target, 'fresh', sym_to_ord => sub { \%values }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | 3 | ▶ | install_modifier $target, 'fresh', ord_to_sym => sub { +{ reverse(%values) } }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 2 | ▶ | install_modifier $target, 'fresh', values => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 2 | my $ord = $_[0]->sym_to_ord; | |||||||||||||||||||
| 57 | 2 | [ sort { $ord->{$a} <=> $ord->{$b} } keys %values ]; | |||||||||||||||||||
| 58 | 3 | }; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 3 | for my $value (keys %values) { | |||||||||||||||||||
| 61 | 13 | ▶ | install_modifier $target, 'fresh', "is_$value" => sub { $_[0]->is($value) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 8 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 68 | 8 | my ($class, $value) = @_; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 8 | ▶ | (blessed($class) || $class)->inflate_symbol($value); | ||||||||||||||||||
|
Condition66%2/3
builtin::blessed($class) || $class
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class)
$class
Truth table66%2/3
A || B
A= builtin::blessed($class)B= $class
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 20 | ▶ | sub inflate_symbol { | ||||||||||||||||||
|
Subroutine
inflate_symbol: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 75 | 20 | my ($class, $symbol) = @_; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 20 | my $ord = $class->sym_to_ord->{$symbol}; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 20 | ▶ | croak "Value [$symbol] is not valid for enum $class" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | unless defined $ord; | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 16 | bless \$ord, $class; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 6 | ▶ | sub inflate_ordinal { | ||||||||||||||||||
|
Subroutine
inflate_ordinal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 87 | 6 | my ($class, $ord) = @_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | croak "Ordinal [$ord] is not valid for enum $class" | ||||||||||||||||||||
| 90 | 6 | ▶ | unless exists $class->ord_to_sym->{$ord}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | bless \$ord, $class; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 2 | ▶ | sub list_is_methods { | ||||||||||||||||||
|
Subroutine
list_is_methods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | 2 | my ($class) = @_; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | map "is_$_", @{$class->values}; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 1 | ▶ | sub type_constraint { | ||||||||||||||||||
|
Subroutine
type_constraint: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 104 | 1 | my ($class) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 1 | require Type::Tiny::Class; | |||||||||||||||||||
| 107 | 1 | require Types::Standard; | |||||||||||||||||||
| 108 | 1 | ▶ | Type::Tiny::Class->new(class => blessed($class) || $class) | ||||||||||||||||||
|
Condition33%1/3
builtin::blessed($class) || $class
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class)
$class
Truth table33%1/3
A || B
A= builtin::blessed($class)B= $class
| |||||||||||||||||||||
| 109 | ->plus_constructors(Types::Standard::Str(), 'inflate_symbol'); | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 3 | ▶ | sub test_symbol { | ||||||||||||||||||
|
Subroutine
test_symbol: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 114 | 3 | my ($class, $value) = @_; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 3 | exists($class->sym_to_ord->{$value}) | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 3 | ▶ | sub test_ordinal { | ||||||||||||||||||
|
Subroutine
test_ordinal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 121 | 3 | my ($class, $value) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 3 | exists($class->ord_to_sym->{$value}) | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 3 | ▶ | sub coerce_symbol { | ||||||||||||||||||
|
Subroutine
coerce_symbol: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 128 | 3 | my ($class, $value) = @_; | |||||||||||||||||||
| 129 | 3 | ▶ | return $value if eval { $value->isa($class) }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | $class->inflate_symbol($value); | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 3 | ▶ | sub coerce_ordinal { | ||||||||||||||||||
|
Subroutine
coerce_ordinal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 136 | 3 | my ($class, $value) = @_; | |||||||||||||||||||
| 137 | 3 | ▶ | return $value if eval { $value->isa($class) }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 2 | $class->inflate_ordinal($value); | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 5 | ▶ | sub coerce_any { | ||||||||||||||||||
|
Subroutine
coerce_any: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 144 | 5 | my ($class, $value) = @_; | |||||||||||||||||||
| 145 | 5 | ▶ | return $value if eval { $value->isa($class) }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 4 | for my $method (qw( inflate_ordinal inflate_symbol )) { | |||||||||||||||||||
| 148 | 7 | my $enum = eval { $class->$method($value) }; | |||||||||||||||||||
| 149 | 7 | ▶ | return $enum if $enum; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | 2 | croak "Could not coerce invalid value [$value] into $class"; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 19 | ▶ | sub is { | ||||||||||||||||||
|
Subroutine
is: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 157 | 19 | my ($self, $value) = @_; | |||||||||||||||||||
| 158 | 19 | my $ord = $self->sym_to_ord->{$value}; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 19 | ▶ | croak "Value [$value] is not valid for enum " . blessed($self) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | unless defined $ord; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 19 | $$self == $ord; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 3 | ▶ | sub stringify { | ||||||||||||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 169 | 3 | my ($self) = @_; | |||||||||||||||||||
| 170 | 3 | $self->ord_to_sym->{$$self}; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 35 | ▶ | sub numify { | ||||||||||||||||||
|
Subroutine
numify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 175 | 35 | my ($self) = @_; | |||||||||||||||||||
| 176 | 35 | $$self; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 9 | ▶ | sub cmp { | ||||||||||||||||||
|
Subroutine
cmp: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 181 | 9 | my ($self, $other, $reversed) = @_; | |||||||||||||||||||
| 182 | 9 | ▶ | return -1 * $self->cmp($other) if $reversed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 8 | ▶ | return $$self <=> $other if blessed($other); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 7 | my $ord = $self->sym_to_ord->{$other}; | |||||||||||||||||||
| 187 | 7 | ▶ | croak "Cannot compare to invalid symbol [$other] for " . blessed($self) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 188 | unless defined $ord; | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 7 | return $$self <=> $ord; | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 1 | ▶ | sub any { | ||||||||||||||||||
|
Subroutine
any: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 195 | 1 | my ($self, @cases) = @_; | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 1 | ▶ | List::Util::any { $self->is($_) } @cases; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 1 | ▶ | sub none { | ||||||||||||||||||
|
Subroutine
none: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 202 | 1 | my ($self, @cases) = @_; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 1 | ▶ | List::Util::none { $self->is($_) } @cases; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 3 | 1; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||