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