refactored to leave synopsis to the top, and move the rest of the pod to the
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
CommitLineData
141459fa 1package Catalyst::ActionChain;
2
3use strict;
4use base qw/Catalyst::Action/;
5
141459fa 6
7=head1 NAME
8
9Catalyst::ActionChain - Chain of Catalyst Actions
10
11=head1 SYNOPSIS
12
b2ddf6d7 13See L<Catalyst::Manual::Intro> for more info about Chained actions.
141459fa 14
15=head1 DESCRIPTION
16
17This class represents a chain of Catalyst Actions. It behaves exactly like
18the action at the *end* of the chain except on dispatch it will execute all
19the actions in the chain in order.
20
b2ddf6d7 21=cut
141459fa 22
b2ddf6d7 23__PACKAGE__->mk_accessors(qw/chain/);
141459fa 24
b2ddf6d7 25use overload (
141459fa 26
b2ddf6d7 27 # Stringify to reverse for debug output etc.
28 q{""} => sub { shift->{reverse} },
141459fa 29
b2ddf6d7 30 # Codulate to execute to invoke the encapsulated action coderef
31 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
32
33 # Make general $stuff still work
34 fallback => 1,
35
36);
141459fa 37
141459fa 38
39sub dispatch {
40 my ( $self, $c ) = @_;
d7962771 41 my @captures = @{$c->req->captures||[]};
ee1ac377 42 my @chain = @{ $self->chain };
43 my $last = pop(@chain);
44 foreach my $action ( @chain ) {
d7962771 45 my @args;
1c34f703 46 if (my $cap = $action->attributes->{CaptureArgs}) {
d7962771 47 @args = splice(@captures, 0, $cap->[0]);
48 }
49 local $c->request->{arguments} = \@args;
141459fa 50 $action->dispatch( $c );
51 }
ee1ac377 52 $last->dispatch( $c );
141459fa 53}
54
b2ddf6d7 55sub from_chain {
56 my ( $self, $actions ) = @_;
57 my $final = $actions->[-1];
58 return $self->new({ %$final, chain => $actions });
59}
60
611;
62
63__END__
64
65=head1 METHODS
66
67=head2 chain
68
69Accessor for the action chain; will be an arrayref of the Catalyst::Action
70objects encapsulated by this chain.
71
72=head2 dispatch( $c )
73
74Dispatch this action chain against a context; will dispatch the encapsulated
75actions in order.
76
141459fa 77=head2 from_chain( \@actions )
78
79Takes a list of Catalyst::Action objects and constructs and returns a
80Catalyst::ActionChain object representing a chain of these actions
81
82=cut
83
141459fa 84=head1 AUTHOR
85
86Matt S. Trout
87
88=head1 COPYRIGHT
89
90This program is free software, you can redistribute it and/or modify it under
91the same terms as Perl itself.
92
93=cut