File Coverage

blib/lib/Catalyst/ActionChain.pm
Criterion Covered Total %
statement 45 53 84.9
branch 15 22 68.1
condition 3 3 100.0
subroutine 7 8 87.5
pod 6 6 100.0
total 76 92 82.6


line stmt bran cond sub pod time code
1              
2             use Moose;
3 102     102   2077 extends qw(Catalyst::Action);
  102         234  
  102         611  
4              
5             has chain => (is => 'rw');
6             no Moose;
7 102     102   546136  
  102         243  
  102         644  
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             my ( $self, $c ) = @_;
25             my @captures = @{$c->req->captures||[]};
26 242     242 1 612 my @chain = @{ $self->chain };
27 242 50       367 my $last = pop(@chain);
  242         582  
28 242         421 foreach my $action ( @chain ) {
  242         6043  
29 242         441 my @args;
30 242         508 if (my $cap = $action->number_of_captures) {
31 290         402 @args = splice(@captures, 0, $cap);
32 290 100       6961 }
33 179         528 local $c->request->{arguments} = \@args;
34             $action->dispatch( $c );
35 290         5753  
36 290         1128 # break the chain if exception occurs in the middle of chain. We
37             # check the global config flag 'abort_chain_on_error_fix', but this
38             # is now considered true by default, so unless someone explicitly sets
39             # it to false we default it to true (if its not defined).
40             my $abort = defined($c->config->{abort_chain_on_error_fix}) ?
41             $c->config->{abort_chain_on_error_fix} : 1;
42             return if ($c->has_errors && $abort);
43 289 100       1190 }
44 289 100 100     1230 $last->dispatch( $c );
45             }
46 237         855  
47             my ( $self, $actions ) = @_;
48             my $final = $actions->[-1];
49             return $self->new({ %$final, chain => $actions });
50 337     337 1 709 }
51 337         555  
52 337         10695 my ( $self ) = @_;
53             my $chain = $self->chain;
54             my $captures = 0;
55              
56 67     67 1 112 $captures += $_->number_of_captures for @$chain;
57 67         1499 return $captures;
58 67         117 }
59              
60 67         1530 my ($self, $c, $captures) = @_;
61 67         149 my @captures = @{$captures||[]};
62              
63             foreach my $link(@{$self->chain}) {
64             my @local_captures = splice @captures,0,$link->number_of_captures;
65 0     0 1 0 return unless $link->match_captures($c, \@local_captures);
66 0 0       0 }
  0         0  
67             return 1;
68 0         0 }
  0         0  
69 0         0 my ($self, $c, $captures) = @_;
70 0 0       0 my @captures = @{$captures||[]};
71              
72 0         0 foreach my $link(@{$self->chain}) {
73             my @local_captures = splice @captures,0,$link->number_of_captures;
74             next unless $link->has_captures_constraints;
75 47     47 1 91 return unless $link->match_captures_constraints($c, \@local_captures);
76 47 50       59 }
  47         143  
77             return 1;
78 47         68 }
  47         1020  
79 116         2463  
80 116 100       3449 # the scheme defined at the end of the chain is the one we use
81 13 100       37 # but warn if too many.
82              
83 44         127 my $self = shift;
84             my @chain = @{ $self->chain };
85             my ($scheme, @more) = map {
86             exists $_->attributes->{Scheme} ? $_->attributes->{Scheme}[0] : ();
87             } reverse @chain;
88              
89             warn "$self is a chain with two many Scheme attributes (only one is allowed)"
90 48     48 1 98 if @more;
91 48         65  
  48         1114  
92             return $scheme;
93 48 100       129 }
  117         2467  
94              
95             __PACKAGE__->meta->make_immutable;
96 48 50       138 1;
97              
98              
99 48         172 =head1 METHODS
100              
101             =head2 chain
102              
103             Accessor for the action chain; will be an arrayref of the Catalyst::Action
104             objects encapsulated by this chain.
105              
106             =head2 dispatch( $c )
107              
108             Dispatch this action chain against a context; will dispatch the encapsulated
109             actions in order.
110              
111             =head2 from_chain( \@actions )
112              
113             Takes a list of Catalyst::Action objects and constructs and returns a
114             Catalyst::ActionChain object representing a chain of these actions
115              
116             =head2 number_of_captures
117              
118             Returns the total number of captures for the entire chain of actions.
119              
120             =head2 match_captures
121              
122             Match all the captures that this chain encloses, if any.
123              
124             =head2 scheme
125              
126             Any defined scheme for the actionchain
127              
128             =head2 meta
129              
130             Provided by Moose
131              
132             =head1 AUTHORS
133              
134             Catalyst Contributors, see Catalyst.pm
135              
136             =head1 COPYRIGHT
137              
138             This library is free software. You can redistribute it and/or modify it under
139             the same terms as Perl itself.
140              
141             =cut