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 ) = @_;
70 #would it be unreasonable to store the number of arguments
71 #the action has as its own attribute?
72 #it would basically eliminate the code below. ehhh. small fish
73 return 1 unless exists $self->attributes->{Args};
74 my $args = $self->attributes->{Args}[0];
75 return 1 unless defined($args) && length($args);
76 return scalar( @{ $c->req->args } ) == $args;
82 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
83 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
85 $_ = looks_like_number($_) ? $_ : ~0
86 for $a1_args, $a2_args;
88 return $a1_args <=> $a2_args;
93 return 0 unless exists $self->attributes->{Args};
94 return $self->attributes->{Args}[0];
97 sub number_of_captures {
100 return 0 unless exists $self->attributes->{CaptureArgs};
101 return $self->attributes->{CaptureArgs}[0] || 0;
104 __PACKAGE__->meta->make_immutable;
114 The sub attributes that are set for this action, like Local, Path, Private
115 and so on. This determines how the action is dispatched to.
119 Returns the name of the component where this action is defined.
120 Derived by calling the L<Catalyst::Component/catalyst_component_name|catalyst_component_name>
121 method on each component.
125 Returns a code reference to this action.
127 =head2 dispatch( $c )
129 Dispatch this action against a context.
131 =head2 execute( $controller, $c, @args )
133 Execute this action's coderef against a given controller with a given
134 context and arguments
138 Check Args attribute, and makes sure number of args matches the setting.
139 Always returns true if Args is omitted.
143 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
144 having the highest precedence.
148 Returns the private namespace this action lives in.
152 Returns the private path for this action.
156 Returns absolute private path for this action. Unlike C<reverse>, the
157 C<private_path> of an action is always suitable for passing to C<forward>.
161 Returns the sub name of this action.
163 =head2 number_of_args
165 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.
167 =head2 number_of_captures
169 Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
175 =head1 OPTIONAL METHODS
177 =head2 match_captures
179 Can be implemented by action class and action role authors. If the method
180 exists, then it will be called with the request context and an array reference
181 of the captures for this action.
183 Returning true from this method causes the chain match to continue, returning
184 makes the chain not match (and alternate, less preferred chains will be attempted).
188 Catalyst Contributors, see Catalyst.pm
192 This library is free software. You can redistribute it and/or modify it under
193 the same terms as Perl itself.