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