Class::Workflow::Transition::Validate - Provide a hook for validating a


Class-Workflow documentation Contained in the Class-Workflow distribution.

Index


Code Index:

NAME

Top

Class::Workflow::Transition::Validate - Provide a hook for validating a transition (conditionals, input validators, etc).

SYNOPSIS

Top

	package MyTransition;
	use Moose;

	with qw/
		Class::Workflow::Transition
		Class::Workflow::Transition::Validate
	/;

	sub validate {
		my ( $self, $instance, %args ) = @_;

		die "only the owner can apply this transition"
			unless $args{user} eq $instance->owner;
	}

DESCRIPTION

Top

This role will call the validate method at the appropriate time.

validate receives the same arguments as apply, and is expected to die if any of the parameters for the transition are invalid.

Technically, this role doesn't do much more than adding syntactic sugar for before 'apply'. However, it's value is in the convention that you can call validate without applying the body. This eases writing side effect free introspection of transitions.


Class-Workflow documentation Contained in the Class-Workflow distribution.

#!/usr/bin/perl

package Class::Workflow::Transition::Validate;
use Moose::Role;

requires "validate";

around apply => sub {
	my $next = shift;
	my ( $self, $instance, @args ) = @_;

	my $error;
	{
		local $@;
		eval { $self->validate( $instance, @args ) };
		$error = $@;
	}

	if ( $error ) {
		return $self->validation_error( $error, $instance, @args );
	} else {
		return $self->$next( $instance, @args );
	}
};

sub validation_error {
	my ( $self, $error, $instance, @args ) = @_;
	die $error;
}

__PACKAGE__;

__END__