1 package Catalyst::Action;
5 Catalyst::Action - Catalyst Action
9 <form action="[%c.uri_for(c.action)%]">
13 This class represents a Catalyst Action. You can access the object for the
14 currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
15 for more information on how actions are dispatched. Actions are defined in
16 L<Catalyst::Controller> subclasses.
21 use Scalar::Util 'looks_like_number';
22 with 'MooseX::Emulate::Class::Accessor::Fast';
23 use namespace::clean -except => 'meta';
25 has class => (is => 'rw');
26 has namespace => (is => 'rw');
27 has 'reverse' => (is => 'rw');
28 has attributes => (is => 'rw');
29 has name => (is => 'rw');
30 has code => (is => 'rw');
34 # Stringify to reverse for debug output etc.
35 q{""} => sub { shift->{reverse} },
37 # Codulate to execute to invoke the encapsulated action coderef
38 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
40 # Make general $stuff still work
47 no warnings 'recursion';
49 #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
51 sub dispatch { # Execute ourselves against a context
52 my ( $self, $c ) = @_;
53 return $c->execute( $self->class, $self );
62 my ( $self, $c ) = @_;
63 #would it be unreasonable to store the number of arguments
64 #the action has as its own attribute?
65 #it would basically eliminate the code below. ehhh. small fish
66 return 1 unless exists $self->attributes->{Args};
67 my $args = $self->attributes->{Args}[0];
68 return 1 unless defined($args) && length($args);
69 return scalar( @{ $c->req->args } ) == $args;
75 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
76 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
78 $_ = looks_like_number($_) ? $_ : ~0
79 for $a1_args, $a2_args;
81 return $a1_args <=> $a2_args;
84 __PACKAGE__->meta->make_immutable;
94 The sub attributes that are set for this action, like Local, Path, Private
95 and so on. This determines how the action is dispatched to.
99 Returns the class name where this action is defined.
103 Returns a code reference to this action.
105 =head2 dispatch( $c )
107 Dispatch this action against a context
109 =head2 execute( $controller, $c, @args )
111 Execute this action's coderef against a given controller with a given
112 context and arguments
116 Check Args attribute, and makes sure number of args matches the setting.
117 Always returns true if Args is omitted.
121 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
122 having the highest precedence.
126 Returns the private namespace this action lives in.
130 Returns the private path for this action.
134 returns the sub name of this action.
142 Catalyst Contributors, see Catalyst.pm
146 This library is free software. You can redistribute it and/or modify it under
147 the same terms as Perl itself.