1 package Catalyst::Action;
5 Catalyst::Action - Catalyst Action
9 <form action="[%c.uri_for(c.action)%]">
11 $c->forward( $action->private_path );
15 This class represents a Catalyst Action. You can access the object for the
16 currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
17 for more information on how actions are dispatched. Actions are defined in
18 L<Catalyst::Controller> subclasses.
23 use Scalar::Util 'looks_like_number';
24 with 'MooseX::Emulate::Class::Accessor::Fast';
25 use namespace::clean -except => 'meta';
27 has class => (is => 'rw');
28 has namespace => (is => 'rw');
29 has 'reverse' => (is => 'rw');
30 has attributes => (is => 'rw');
31 has name => (is => 'rw');
32 has code => (is => 'rw');
34 reader => 'private_path',
38 default => sub { '/'.shift->reverse },
43 # Stringify to reverse for debug output etc.
44 q{""} => sub { shift->{reverse} },
46 # Codulate to execute to invoke the encapsulated action coderef
47 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
49 # Make general $stuff still work
56 no warnings 'recursion';
58 sub dispatch { # Execute ourselves against a context
59 my ( $self, $c ) = @_;
60 return $c->execute( $self->class, $self );
69 my ( $self, $c, $captures ) = @_;
70 ## It would seem that now that we can match captures, we could remove a lot
71 ## of the capture_args to args mapping all around. I gave it a go, but was
72 ## not trival, contact jnap on irc for what I tried if you want to try.
73 ## return $self->_match_has_expected_capture_args($captures) &&
74 return $self->_match_has_expected_http_method($c->req->method);
78 my ( $self, $c ) = @_;
79 return $self->_match_has_expected_args($c->req->args) &&
80 $self->_match_has_expected_http_method($c->req->method);
83 sub _match_has_expected_args {
84 my ($self, $req_args) = @_;
85 return 1 unless exists $self->attributes->{Args};
86 my $args = $self->attributes->{Args}[0];
87 return 1 unless defined($args) && length($args);
88 return scalar( @{$req_args} ) == $args;
91 sub _match_has_expected_capture_args {
92 my ($self, $req_args) = @_;
93 return 1 unless exists $self->attributes->{CaptureArgs};
94 my $args = $self->attributes->{CaptureArgs}[0];
95 return 1 unless defined($args) && length($args);
96 return scalar( @{$req_args} ) == $args;
99 sub _match_has_expected_http_method {
100 my ($self, $method) = @_;
101 my @methods = @{ $self->attributes->{Method} || [] };
102 if(scalar @methods) {
103 my $result = scalar(grep { lc($_) eq lc($method) } @methods) ? 1:0;
106 ## No HTTP Methods to check
114 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
115 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
117 $_ = looks_like_number($_) ? $_ : ~0
118 for $a1_args, $a2_args;
120 return $a1_args <=> $a2_args;
125 return 0 unless exists $self->attributes->{Args};
126 return $self->attributes->{Args}[0];
129 sub number_of_captures {
132 return 0 unless exists $self->attributes->{CaptureArgs};
133 return $self->attributes->{CaptureArgs}[0] || 0;
136 __PACKAGE__->meta->make_immutable;
146 The sub attributes that are set for this action, like Local, Path, Private
147 and so on. This determines how the action is dispatched to.
151 Returns the name of the component where this action is defined.
152 Derived by calling the L<Catalyst::Component/catalyst_component_name|catalyst_component_name>
153 method on each component.
157 Returns a code reference to this action.
159 =head2 dispatch( $c )
161 Dispatch this action against a context.
163 =head2 execute( $controller, $c, @args )
165 Execute this action's coderef against a given controller with a given
166 context and arguments
170 Check Args attribute, and makes sure number of args matches the setting.
171 Always returns true if Args is omitted.
173 =head2 match_captures ($c, $captures)
175 Can be implemented by action class and action role authors. If the method
176 exists, then it will be called with the request context and an array reference
177 of the captures for this action.
179 Returning true from this method causes the chain match to continue, returning
180 makes the chain not match (and alternate, less preferred chains will be attempted).
185 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
186 having the highest precedence.
190 Returns the private namespace this action lives in.
194 Returns the private path for this action.
198 Returns absolute private path for this action. Unlike C<reverse>, the
199 C<private_path> of an action is always suitable for passing to C<forward>.
203 Returns the sub name of this action.
205 =head2 number_of_args
207 Returns the number of args this action expects. This is 0 if the action doesn't take any arguments and undef if it will take any number of arguments.
209 =head2 number_of_captures
211 Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
219 Catalyst Contributors, see Catalyst.pm
223 This library is free software. You can redistribute it and/or modify it under
224 the same terms as Perl itself.