| 1 | package MooseX::Attribute::TypeConstraint::CustomizeFatal; | ||||||||||||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | $MooseX::Attribute::TypeConstraint::CustomizeFatal::AUTHORITY = 'cpan:AVAR'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | { | ||||||||||||||||||||
| 6 | 2 | $MooseX::Attribute::TypeConstraint::CustomizeFatal::VERSION = '0.03'; | |||||||||||||||||||
| 7 | } | ||||||||||||||||||||
| 8 | 2 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use MooseX::Types::Moose ':all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Carp qw(confess); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use MooseX::Types -declare => [ qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | TypeConstraintCustomizeFatalAction | ||||||||||||||||||||
| 13 | 2 | ) ]; | |||||||||||||||||||
| 14 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 2 | enum TypeConstraintCustomizeFatalAction, [ qw( | |||||||||||||||||||
| 17 | error | ||||||||||||||||||||
| 18 | warning | ||||||||||||||||||||
| 19 | default | ||||||||||||||||||||
| 20 | default_no_warning | ||||||||||||||||||||
| 21 | ) ]; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | has on_typeconstraint_failure => ( | ||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||
| 25 | isa => TypeConstraintCustomizeFatalAction, | ||||||||||||||||||||
| 26 | default => 'error', | ||||||||||||||||||||
| 27 | 2 | documentation => "What should we do when a typeconstraint fails? Possible values: " . join(", ", @{ TypeConstraintCustomizeFatalAction->values }), | |||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 30 | ▶ | around _coerce_and_verify => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||
| 31 | 30 | my $orig = shift; | |||||||||||||||||||
| 32 | 30 | my $self = shift; | |||||||||||||||||||
| 33 | 30 | my $val = shift; | |||||||||||||||||||
| 34 | 30 | my @args = @_; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 30 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 30 | $self->$orig($val => @args); | |||||||||||||||||||
| 38 | 4 | ▶ | } catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=23.8
| |||||||||||||||||||||
| 39 | 4 | my $error = $_; | |||||||||||||||||||
| 40 | 4 | ▶ | if ($error =~ /does not pass the type constraint/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 4 | my $action = $self->on_typeconstraint_failure; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 4 | ▶ | if ($action eq 'error') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 44 | # Just die like Moose does by default | ||||||||||||||||||||
| 45 | 1 | die $error; | |||||||||||||||||||
| 46 | } elsif ($action eq 'warning') { | ||||||||||||||||||||
| 47 | # Warn but keep the current value. | ||||||||||||||||||||
| 48 | 1 | warn $error; | |||||||||||||||||||
| 49 | 1 | return $val; | |||||||||||||||||||
| 50 | ▶ | } elsif ($action eq 'default' or | |||||||||||||||||||
|
Condition66%2/3
$action eq "default" or $action eq "default_no_warning"
MC/DC0%0/2
$action eq "default" or $action eq "default_no_warning"
$action eq "default"
$action eq "default_no_warning"
Truth table66%2/3
A or B
A= $action eq "default"B= $action eq "default_no_warning"
| |||||||||||||||||||||
| 51 | $action eq 'default_no_warning') { | ||||||||||||||||||||
| 52 | 2 | ▶ | warn $error unless $action eq 'default_no_warning'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 2 | ▶ | if ($self->has_default) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 2 | my $default = $self->default; | |||||||||||||||||||
| 55 | 2 | ▶ | return ref $default eq 'CODE' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | # If it's e.g. sub { [] } | ||||||||||||||||||||
| 57 | ? $default->() | ||||||||||||||||||||
| 58 | # It's just a normal SV, e.g. "12345" | ||||||||||||||||||||
| 59 | : $default; | ||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||
| 61 | 0 | confess( | |||||||||||||||||||
| 62 | "Attribute (" | ||||||||||||||||||||
| 63 | . $self->name | ||||||||||||||||||||
| 64 | . ") does not have a default value to fall back to" | ||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||
| 68 | 0 | die "PANIC: Unknown action <$action>"; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | return $val; | |||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||
| 73 | 0 | die $error; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 30 | }; | |||||||||||||||||||
| 76 | 2 | }; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | package Moose::Meta::Attribute::Custom::Trait::TypeConstraint::CustomizeFatal; | ||||||||||||||||||||
| 79 | 2 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 80 | 2 | $Moose::Meta::Attribute::Custom::Trait::TypeConstraint::CustomizeFatal::AUTHORITY = 'cpan:AVAR'; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | { | ||||||||||||||||||||
| 83 | 2 | $Moose::Meta::Attribute::Custom::Trait::TypeConstraint::CustomizeFatal::VERSION = '0.03'; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 1 | ▶ | sub register_implementation { 'MooseX::Attribute::TypeConstraint::CustomizeFatal' } | ||||||||||||||||||
|
Subroutine
register_implementation: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||