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