package CGI::Application::Plugin::Dispatch;

use strict;
use Carp;
use base 'Exporter';
our $VERSION = '0.01';
our @EXPORT = qw(post_dispatch dispatch _dispatch_init);

sub import{
	my $caller = scalar(caller);
    no strict 'refs';
    foreach my $sym (@EXPORT) {
        *{"${caller}::$sym"} = \&{$sym};
    }
	$caller->add_callback('init', '_dispatch_init');
	goto &Exporter::import;
}

sub post_dispatch{
	my $self = shift;
	my $newval = shift;
	my $valname = "CGI::Application::Plugin::Dispatch::__post_dispatch";
	$self->{$valname} = 1 unless exists $self->{$valname};
	if(defined $newval){
		$self->{$valname} = $newval;
	}
	return $self->{$valname};
}

sub _dispatch_init{
	my $self = shift;
	$self->run_modes(
		'AUTOLOAD'=>'dispatch'
	);
}

sub dispatch{
	my $self = shift;
	my $rm = shift;
	
	my $postmeth = 'post_dispatch_' . $rm;
	if($self->post_dispatch && 
		lc($self->query->request_method) eq 'post' && 
		$self->can($postmeth)){
			return $self->$postmeth();
	}
	my $method = 'dispatch_' . $rm;
	return $self->$method();
}


1;


__END__



