| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Class::Workflow::Transition::Validate::Simple; | ||||||||||||||||||||
| 4 | 5 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 5 | with qw/ | |||||||||||||||||||
| 7 | Class::Workflow::Transition | ||||||||||||||||||||
| 8 | Class::Workflow::Transition::Validate | ||||||||||||||||||||
| 9 | /; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 5 | has error_state => ( | |||||||||||||||||||
| 12 | does => "Class::Workflow::State", | ||||||||||||||||||||
| 13 | is => "rw", | ||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | has no_die => ( | |||||||||||||||||||
| 17 | isa => "Bool", | ||||||||||||||||||||
| 18 | is => "rw", | ||||||||||||||||||||
| 19 | default => 0, | ||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | has validators => ( | ||||||||||||||||||||
| 23 | isa => "ArrayRef", | ||||||||||||||||||||
| 24 | is => "rw", | ||||||||||||||||||||
| 25 | auto_deref => 1, | ||||||||||||||||||||
| 26 | 24 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 5 | ); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 5 | has ignore_validator_rv => ( | |||||||||||||||||||
| 30 | isa => "Bool", | ||||||||||||||||||||
| 31 | is => "rw", | ||||||||||||||||||||
| 32 | default => 0, | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | sub add_validators { | ||||||||||||||||||
|
Subroutine
add_validators: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 36 | 0 | my ( $self, @validators ) = @_; | |||||||||||||||||||
| 37 | 0 | push @{ $self->validators }, @validators; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub clear_validators { | ||||||||||||||||||
|
Subroutine
clear_validators: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||
| 42 | 0 | $self->validators([]); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 11 | ▶ | sub validate { | ||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||
| 46 | 11 | my ( $self, $instance, @args ) = @_; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 11 | my $ignore_rv = $self->ignore_validator_rv; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 11 | my @errors; | |||||||||||||||||||
| 51 | 11 | foreach my $validator ( $self->validators ) { | |||||||||||||||||||
| 52 | 3 | local $@; | |||||||||||||||||||
| 53 | 3 | my $ok = eval { $self->$validator( $instance, @args ) }; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 3 | ▶ | if ( $@ ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 56 | 1 | push @errors, $@; | |||||||||||||||||||
| 57 | ▶ | } elsif ( !$ignore_rv and !$ok ) { | |||||||||||||||||||
|
Condition33%1/3
$ignore_rv || $ok
MC/DC0%0/2
$ignore_rv || $ok
$ignore_rv
$ok
Truth table33%1/3
A || B
A= $ignore_rvB= $ok
| |||||||||||||||||||||
| 58 | 0 | push @errors, "general error"; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 11 | s/ at .* line \d+\.\n//s for @errors; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 11 | ▶ | die join(", ", @errors) . "\n" if @errors; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | # FIXME add @errors to an exception object that stringifies | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 10 | return 1; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | ▶ | sub validation_error { | ||||||||||||||||||
|
Subroutine
validation_error: called CC=4 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||
| 71 | 1 | my ( $self, $error, $instance, @args ) = @_; | |||||||||||||||||||
| 72 | 1 | ▶ | chomp $error unless ref $error; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | ▶ | if ( my $state = $self->error_state ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 1 | return $self->derive_and_accept_instance( | |||||||||||||||||||
| 76 | $instance => { | ||||||||||||||||||||
| 77 | state => $state, | ||||||||||||||||||||
| 78 | error => $error | ||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||
| 80 | @args | ||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||
| 83 | 0 | ▶ | if ( $self->no_die ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | return $self->derive_and_accept_instance( | |||||||||||||||||||
| 85 | $instance => { | ||||||||||||||||||||
| 86 | state => $instance->state, | ||||||||||||||||||||
| 87 | error => $error, | ||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||
| 89 | @args | ||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||
| 92 | 0 | die $error; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 5 | __PACKAGE__; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||