Refreshing branch
[catagits/Catalyst-Runtime.git] / trunk / lib / Catalyst / ActionChain.pm
CommitLineData
ceae39c5 1package Catalyst::ActionChain;
2
3use Moose;
4extends qw(Catalyst::Action);
5
6has chain => (is => 'rw');
7
8no Moose;
9
10=head1 NAME
11
12Catalyst::ActionChain - Chain of Catalyst Actions
13
14=head1 SYNOPSIS
15
16See L<Catalyst::Manual::Intro> for more info about Chained actions.
17
18=head1 DESCRIPTION
19
20This class represents a chain of Catalyst Actions. It behaves exactly like
21the action at the *end* of the chain except on dispatch it will execute all
22the actions in the chain in order.
23
24=cut
25
26sub dispatch {
27 my ( $self, $c ) = @_;
28 my @captures = @{$c->req->captures||[]};
29 my @chain = @{ $self->chain };
30 my $last = pop(@chain);
31 foreach my $action ( @chain ) {
32 my @args;
33 if (my $cap = $action->attributes->{CaptureArgs}) {
34 @args = splice(@captures, 0, $cap->[0]);
35 }
36 local $c->request->{arguments} = \@args;
37 $action->dispatch( $c );
38 }
39 $last->dispatch( $c );
40}
41
42sub from_chain {
43 my ( $self, $actions ) = @_;
44 my $final = $actions->[-1];
45 return $self->new({ %$final, chain => $actions });
46}
47
48__PACKAGE__->meta->make_immutable;
491;
50
51__END__
52
53=head1 METHODS
54
55=head2 chain
56
57Accessor for the action chain; will be an arrayref of the Catalyst::Action
58objects encapsulated by this chain.
59
60=head2 dispatch( $c )
61
62Dispatch this action chain against a context; will dispatch the encapsulated
63actions in order.
64
65=head2 from_chain( \@actions )
66
67Takes a list of Catalyst::Action objects and constructs and returns a
68Catalyst::ActionChain object representing a chain of these actions
69
70=head2 meta
71
72Provided by Moose
73
74=head1 AUTHORS
75
76Catalyst Contributors, see Catalyst.pm
77
78=head1 COPYRIGHT
79
80This library is free software. You can redistribute it and/or modify it under
81the same terms as Perl itself.
82
83=cut