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 use Moose::Util::TypeConstraints ();
25 with 'MooseX::Emulate::Class::Accessor::Fast';
26 use namespace::clean -except => 'meta';
28 has class => (is => 'rw');
29 has namespace => (is => 'rw');
30 has 'reverse' => (is => 'rw');
31 has attributes => (is => 'rw');
32 has name => (is => 'rw');
33 has code => (is => 'rw');
35 reader => 'private_path',
39 default => sub { '/'.shift->reverse },
42 has args_constraints => (
48 builder=>'_build_args_constraints',
50 has_args_constraints => 'count',
51 number_of_args => 'count',
52 all_args_constraints => 'elements',
55 sub _build_args_constraints {
57 my @arg_protos = @{$self->attributes->{Args}||[]};
59 return [] unless scalar(@arg_protos);
60 # If there is only one arg and it looks like a number
61 # we assume its 'classic' and the number is the number of
65 scalar(@arg_protos) == 1 &&
66 looks_like_number($arg_protos[0])
70 @args = map { Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) || die "$_ is not a constraint!" } @arg_protos;
78 # Stringify to reverse for debug output etc.
79 q{""} => sub { shift->{reverse} },
81 # Codulate to execute to invoke the encapsulated action coderef
82 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
84 # Make general $stuff still work
91 no warnings 'recursion';
93 sub dispatch { # Execute ourselves against a context
94 my ( $self, $c ) = @_;
95 return $c->execute( $self->class, $self );
104 my ( $self, $c ) = @_;
105 warn "number args = ${\$self->number_of_args} for ${\$self->name}";
106 return 1 unless $self->number_of_args;
107 #my $args = $self->attributes->{Args}[0];
108 #return 1 unless defined($args) && length($args); The "Args" slurpy case, remove for now.
109 if( scalar( @{ $c->req->args } ) == $self->number_of_args ) {
110 return 1 unless $self->has_args_constraints;
111 for my $i($#{ $c->req->args }) {
112 $self->args_constraints->[$i]->check($c->req->args->[$i]) || return 0;
120 sub match_captures { 1 }
124 my ($a1_args) = $a1->number_of_args;
125 my ($a2_args) = $a2->number_of_args;
127 return $a1_args <=> $a2_args;
130 sub number_of_captures {
133 return 0 unless exists $self->attributes->{CaptureArgs};
134 return $self->attributes->{CaptureArgs}[0] || 0;
138 return exists $_[0]->attributes->{Scheme} ? $_[0]->attributes->{Scheme}[0] : undef;
141 sub list_extra_info {
144 Args => $self->attributes->{Args}[0],
145 CaptureArgs => $self->number_of_captures,
149 __PACKAGE__->meta->make_immutable;
159 The sub attributes that are set for this action, like Local, Path, Private
160 and so on. This determines how the action is dispatched to.
164 Returns the name of the component where this action is defined.
165 Derived by calling the L<catalyst_component_name|Catalyst::Component/catalyst_component_name>
166 method on each component.
170 Returns a code reference to this action.
172 =head2 dispatch( $c )
174 Dispatch this action against a context.
176 =head2 execute( $controller, $c, @args )
178 Execute this action's coderef against a given controller with a given
179 context and arguments
183 Check Args attribute, and makes sure number of args matches the setting.
184 Always returns true if Args is omitted.
186 =head2 match_captures ($c, $captures)
188 Can be implemented by action class and action role authors. If the method
189 exists, then it will be called with the request context and an array reference
190 of the captures for this action.
192 Returning true from this method causes the chain match to continue, returning
193 makes the chain not match (and alternate, less preferred chains will be attempted).
198 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
199 having the highest precedence.
203 Returns the private namespace this action lives in.
207 Returns the private path for this action.
211 Returns absolute private path for this action. Unlike C<reverse>, the
212 C<private_path> of an action is always suitable for passing to C<forward>.
216 Returns the sub name of this action.
218 =head2 number_of_args
220 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.
222 =head2 number_of_captures
224 Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
226 =head2 list_extra_info
228 A HashRef of key-values that an action can provide to a debugging screen
232 Any defined scheme for the action
240 Catalyst Contributors, see Catalyst.pm
244 This library is free software. You can redistribute it and/or modify it under
245 the same terms as Perl itself.