File Coverage

blib/lib/Class/Workflow/Transition/Validate.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Class::Workflow::Transition::Validate;
4 1     1   1333 use Moose::Role;
  0            
  0            
5              
6             requires "validate";
7              
8             around apply => sub {
9             my $next = shift;
10             my ( $self, $instance, @args ) = @_;
11              
12             my $error;
13             {
14             local $@;
15             eval { $self->validate( $instance, @args ) };
16             $error = $@;
17             }
18              
19             if ( $error ) {
20             return $self->validation_error( $error, $instance, @args );
21             } else {
22             return $self->$next( $instance, @args );
23             }
24             };
25              
26             sub validation_error {
27             my ( $self, $error, $instance, @args ) = @_;
28             die $error;
29             }
30              
31             __PACKAGE__;
32              
33             __END__
34              
35             =pod
36              
37             =head1 NAME
38              
39             Class::Workflow::Transition::Validate - Provide a hook for validating a
40             transition (conditionals, input validators, etc).
41              
42             =head1 SYNOPSIS
43              
44             package MyTransition;
45             use Moose;
46              
47             with qw/
48             Class::Workflow::Transition
49             Class::Workflow::Transition::Validate
50             /;
51              
52             sub validate {
53             my ( $self, $instance, %args ) = @_;
54              
55             die "only the owner can apply this transition"
56             unless $args{user} eq $instance->owner;
57             }
58              
59             =head1 DESCRIPTION
60              
61             This role will call the C<validate> method at the appropriate time.
62              
63             C<validate> receives the same arguments as C<apply>, and is expected to die if
64             any of the parameters for the transition are invalid.
65              
66             Technically, this role doesn't do much more than adding syntactic sugar for
67             C<before 'apply'>. However, it's value is in the convention that you can call
68             C<validate> without applying the body. This eases writing side effect free
69             introspection of transitions.
70              
71             =cut
72              
73