File Coverage

blib/lib/Catalyst/ActionChain.pm
Criterion Covered Total %
statement 26 29 89.7
branch 2 4 50.0
condition n/a
subroutine 6 7 85.7
pod 2 2 100.0
total 36 42 85.7


line stmt bran cond sub pod time code
1             package Catalyst::ActionChain;
2              
3 46     46   1630 use strict;
  46         486  
  46         703  
4 46     46   768 use base qw/Catalyst::Action/;
  46         444  
  46         2038  
5              
6              
7             =head1 NAME
8            
9             Catalyst::ActionChain - Chain of Catalyst Actions
10            
11             =head1 SYNOPSIS
12            
13             See L<Catalyst::Manual::Intro> for more info about Chained actions.
14            
15             =head1 DESCRIPTION
16            
17             This class represents a chain of Catalyst Actions. It behaves exactly like
18             the action at the *end* of the chain except on dispatch it will execute all
19             the actions in the chain in order.
20            
21             =cut
22              
23             __PACKAGE__->mk_accessors(qw/chain/);
24              
25             use overload (
26              
27             # Stringify to reverse for debug output etc.
28 960     960   17905     q{""} => sub { shift->{reverse} },
29              
30             # Codulate to execute to invoke the encapsulated action coderef
31 0     0   0     '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
  0         0  
  0         0  
32              
33             # Make general $stuff still work
34 46         7720     fallback => 1,
35              
36 46     46   1018 );
  46         1228  
37              
38              
39             sub dispatch {
40 236     236 1 7069     my ( $self, $c ) = @_;
41 236 50       2370     my @captures = @{$c->req->captures||[]};
  236         5002  
42 236         4752     my @chain = @{ $self->chain };
  236         3535  
43 236         5001     my $last = pop(@chain);
44 236         2474     foreach my $action ( @chain ) {
45 230         1943         my @args;
46 230 50       13982         if (my $cap = $action->attributes->{CaptureArgs}) {
47 230         6272           @args = splice(@captures, 0, $cap->[0]);
48                     }
49 230         3844         local $c->request->{arguments} = \@args;
50 230         8275         $action->dispatch( $c );
51                 }
52 234         8492     $last->dispatch( $c );
53             }
54              
55             sub from_chain {
56 240     240 1 2391     my ( $self, $actions ) = @_;
57 240         2410     my $final = $actions->[-1];
58 240         8496     return $self->new({ %$final, chain => $actions });
59             }
60              
61             1;
62              
63             __END__
64            
65             =head1 METHODS
66            
67             =head2 chain
68            
69             Accessor for the action chain; will be an arrayref of the Catalyst::Action
70             objects encapsulated by this chain.
71            
72             =head2 dispatch( $c )
73            
74             Dispatch this action chain against a context; will dispatch the encapsulated
75             actions in order.
76            
77             =head2 from_chain( \@actions )
78            
79             Takes a list of Catalyst::Action objects and constructs and returns a
80             Catalyst::ActionChain object representing a chain of these actions
81            
82             =cut
83            
84             =head1 AUTHOR
85            
86             Matt S. Trout
87            
88             =head1 COPYRIGHT
89            
90             This program is free software, you can redistribute it and/or modify it under
91             the same terms as Perl itself.
92            
93             =cut
94