Fixed run-on sentence in COPYRIGHT and s/program/library/
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionChain.pm
1 package Catalyst::ActionChain;
2
3 use Moose;
4 extends qw(Catalyst::Action);
5
6 has chain => (is => 'rw');
7
8 no Moose;
9
10 =head1 NAME
11
12 Catalyst::ActionChain - Chain of Catalyst Actions
13
14 =head1 SYNOPSIS
15
16 See L<Catalyst::Manual::Intro> for more info about Chained actions.
17
18 =head1 DESCRIPTION
19
20 This class represents a chain of Catalyst Actions. It behaves exactly like
21 the action at the *end* of the chain except on dispatch it will execute all
22 the actions in the chain in order.
23
24 =cut
25
26 sub 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
42 sub 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;
49 1;
50
51 __END__
52
53 =head1 METHODS
54
55 =head2 chain
56
57 Accessor for the action chain; will be an arrayref of the Catalyst::Action
58 objects encapsulated by this chain.
59
60 =head2 dispatch( $c )
61
62 Dispatch this action chain against a context; will dispatch the encapsulated
63 actions in order.
64
65 =head2 from_chain( \@actions )
66
67 Takes a list of Catalyst::Action objects and constructs and returns a
68 Catalyst::ActionChain object representing a chain of these actions
69
70 =head2 meta
71
72 Provided by Moose
73
74 =head1 AUTHORS
75
76 Catalyst Contributors, see Catalyst.pm
77
78 =head1 COPYRIGHT
79
80 This library is free software. You can redistribute it and/or modify it under
81 the same terms as Perl itself.
82
83 =cut