1#!/usr/bin/perl
2
3package Class::Workflow::Transition::Validate;
4use Moose::Role;
5
6requires "validate";
7
8around 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
26sub 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
39Class::Workflow::Transition::Validate - Provide a hook for validating a
40transition (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
61This role will call the C<validate> method at the appropriate time.
62
63C<validate> receives the same arguments as C<apply>, and is expected to die if
64any of the parameters for the transition are invalid.
65
66Technically, this role doesn't do much more than adding syntactic sugar for
67C<before 'apply'>. However, it's value is in the convention that you can call
68C<validate> without applying the body. This eases writing side effect free
69introspection of transitions.
70
71=cut
72
73
74