| 1 | package Unexpected::TraitFor::ExceptionClasses; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Unexpected::Functions qw( inflate_message ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | my $ROOT = 'Unexpected'; my $Classes = { $ROOT => {} }; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | __PACKAGE__->add_exception( 'Unspecified' => { | |||||||||||||||||||||||||||||||||||||||||
| 11 | parents => $ROOT, error => 'Parameter [_1] not specified' } ); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # Public attributes | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 27 | ▶ | has 'class' => is => 'ro', isa => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 27 | ▶ | ($_[ 0 ] and exists $Classes->{ $_[ 0 ] }) or die inflate_message | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and exists $$Classes{$_[0]}
MC/DC50%1/2
$_[0] and exists $$Classes{$_[0]}
$_[0]
exists $$Classes{$_[0]}
Truth table66%2/3
A and B
A= $_[0]B= exists $$Classes{$_[0]}
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ( 'Exception class [_1] does not exist', $_[ 0 ] ) }, default => $ROOT; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Construction | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 28 | ▶ | around 'BUILDARGS' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 28 | my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args ); my $class; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 28 | ▶ | (exists $attr->{class} and $class = $attr->{class}) or return $attr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $attr->{"class"} and $class = $attr->{"class"}
MC/DC50%1/2
exists $attr->{"class"} and $class = $attr->{"class"}
exists $attr->{"class"}
$class = $attr->{"class"}
Truth table66%2/3
A and B
A= exists $attr->{"class"}B= $class = $attr->{"class"}
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 14 | ▶ | ref $class eq 'CODE' and $class = $attr->{class} = $class->(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 14 | ▶ | $self->is_exception( $class ) or return $attr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | for my $k (grep { ! m{ \A parents \z }mx } keys %{ $Classes->{ $class } }) { | |||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | ▶ | $attr->{ $k } //= $Classes->{ $class }->{ $k }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$attr->{$k} //= $Classes->{$class}{$k}
MC/DC0%0/2
$attr->{$k} //= $Classes->{$class}{$k}
$attr->{$k}
$Classes->{$class}{$k}
Truth table66%2/3
A //= B
A= $attr->{$k}B= $Classes->{$class}{$k}
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 13 | return $attr; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # Public class methods | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | sub add_exception { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_exception: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | ▶ | my ($self, $class, $args) = @_; $args //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args //= {}
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 15 | ▶ | defined $class or die "Parameter 'exception class' not specified"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 14 | ▶ | exists $Classes->{ $class } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | and die "Exception class ${class} already exists"; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | ▶ | ref $args ne 'HASH' and $args = { parents => $args }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 13 | ▶ | my $parents = $args->{parents} //= [ $ROOT ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"parents"} //= [$ROOT]
MC/DC100%1/1
$args->{"parents"} //= [$ROOT]
$args->{"parents"}
Truth table100%2/2
A
A= $args->{"parents"}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 13 | ▶ | ref $parents ne 'ARRAY' and $parents = $args->{parents} = [ $parents ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 13 | for my $parent (@{ $parents }) { | |||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | ▶ | exists $Classes->{ $parent } or die | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | "Exception class ${class} parent class ${parent} does not exist"; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 12 | $Classes->{ $class } = $args; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 12 | return; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 22 | ▶ | sub is_exception { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_exception: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 22 | ▶ | return $_[ 1 ] && !ref $_[ 1 ] && exists $Classes->{ $_[ 1 ] } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$_[1] and not ref $_[1] and exists $$Classes{$_[1]}
$_[1] and not ref $_[1]
MC/DC66%2/3
$_[1] and not ref $_[1] and exists $$Classes{$_[1]}
$_[1]
not ref $_[1]
exists $$Classes{$_[1]}
Truth table75%3/4
A and not(B) and C
A= $_[1]B= not ref $_[1]C= exists $$Classes{$_[1]}
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # Public object methods | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 11 | ▶ | sub instance_of { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instance_of: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | ▶ | my ($self, $wanted) = @_; $wanted or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | ref $wanted eq 'CODE' and $wanted = $wanted->(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | exists $Classes->{ $wanted } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | or die "Exception class ${wanted} does not exist"; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | my @classes = ( $self->class ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | while (defined (my $class = shift @classes)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 22 | ▶ | $class eq $wanted and return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | exists $Classes->{ $class }->{parents} | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 15 | ▶ | and push @classes, @{ $Classes->{ $class }->{parents} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||||||||||||||||