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   2388 extends qw(Catalyst::Action);
  102         272  
  102         652  
4              
5             has chain => (is => 'rw');
6             no Moose;
7 102     102   649455  
  102         286  
  102         568  
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 759 my @chain = @{ $self->chain };
27 242 50       487 my $last = pop(@chain);
  242         767  
28 242         668 foreach my $action ( @chain ) {
  242         7077  
29 242         638 my @args;
30 242         734 if (my $cap = $action->number_of_captures) {
31 290         531 @args = splice(@captures, 0, $cap);
32 290 100       8410 }
33 179         688 local $c->request->{arguments} = \@args;
34             $action->dispatch( $c );
35 290         6988  
36 290         1259 # 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       1347 }
44 289 100 100     1513 $last->dispatch( $c );
45             }
46 237         990  
47             my ( $self, $actions ) = @_;
48             my $final = $actions->[-1];
49             return $self->new({ %$final, chain => $actions });
50 337     337 1 1007 }
51 337         818  
52 337         13380 my ( $self ) = @_;
53             my $chain = $self->chain;
54             my $captures = 0;
55              
56 67     67 1 144 $captures += $_->number_of_captures for @$chain;
57 67         1858 return $captures;
58 67         130 }
59              
60 67         1844 my ($self, $c, $captures) = @_;
61 67         560 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 113 return unless $link->match_captures_constraints($c, \@local_captures);
76 47 50       80 }
  47         184  
77             return 1;
78 47         95 }
  47         1283  
79 116         3050  
80 116 100       4185 # the scheme defined at the end of the chain is the one we use
81 13 100       49 # but warn if too many.
82              
83 44         168 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 100 if @more;
91 48         82  
  48         1331  
92             return $scheme;
93 48 100       122 }
  117         2931  
94              
95             __PACKAGE__->meta->make_immutable;
96 48 50       145 1;
97              
98              
99 48         184 =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