Fixed a bunch of POD hyperlinks in Catalyst.pm and minor punctuation in Catalyst...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
CommitLineData
fbcc39ad 1package Catalyst::Action;
2
b2ddf6d7 3=head1 NAME
4
5Catalyst::Action - Catalyst Action
6
7=head1 SYNOPSIS
8
804fb55d 9 <form action="[%c.uri_for(c.action)%]">
85d9fce6 10
009b5b23 11 $c->forward( $action->private_path );
12
b2ddf6d7 13=head1 DESCRIPTION
14
43c58153 15This class represents a Catalyst Action. You can access the object for the
b2ddf6d7 16currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
17for more information on how actions are dispatched. Actions are defined in
18L<Catalyst::Controller> subclasses.
19
20=cut
21
059c085b 22use Moose;
05b47f2e 23use Scalar::Util 'looks_like_number';
241edc9b 24with 'MooseX::Emulate::Class::Accessor::Fast';
05b47f2e 25use namespace::clean -except => 'meta';
241edc9b 26
5fb12dbb 27has class => (is => 'rw');
28has namespace => (is => 'rw');
29has 'reverse' => (is => 'rw');
30has attributes => (is => 'rw');
31has name => (is => 'rw');
32has code => (is => 'rw');
009b5b23 33has private_path => (
34 reader => 'private_path',
35 isa => 'Str',
36 lazy => 1,
37 required => 1,
38 default => sub { '/'.shift->reverse },
39);
059c085b 40
2055d9ad 41use overload (
42
43 # Stringify to reverse for debug output etc.
44 q{""} => sub { shift->{reverse} },
45
46 # Codulate to execute to invoke the encapsulated action coderef
47 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
48
49 # Make general $stuff still work
50 fallback => 1,
51
52);
53
54
55
059c085b 56no warnings 'recursion';
57
58#__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
fbcc39ad 59
b2ddf6d7 60sub dispatch { # Execute ourselves against a context
61 my ( $self, $c ) = @_;
049f82e2 62 return $c->execute( $self->class, $self );
b2ddf6d7 63}
fbcc39ad 64
b2ddf6d7 65sub execute {
66 my $self = shift;
059c085b 67 $self->code->(@_);
b2ddf6d7 68}
fbcc39ad 69
b2ddf6d7 70sub match {
71 my ( $self, $c ) = @_;
e5ecd5bc 72 #would it be unreasonable to store the number of arguments
43c58153 73 #the action has as its own attribute?
e5ecd5bc 74 #it would basically eliminate the code below. ehhh. small fish
b2ddf6d7 75 return 1 unless exists $self->attributes->{Args};
76 my $args = $self->attributes->{Args}[0];
77 return 1 unless defined($args) && length($args);
78 return scalar( @{ $c->req->args } ) == $args;
79}
fbcc39ad 80
05b47f2e 81sub compare {
82 my ($a1, $a2) = @_;
83
cbe555e8 84 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
85 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
86
18a9655c 87 $_ = looks_like_number($_) ? $_ : ~0
cbe555e8 88 for $a1_args, $a2_args;
89
90 return $a1_args <=> $a2_args;
05b47f2e 91}
92
e5ecd5bc 93__PACKAGE__->meta->make_immutable;
94
b2ddf6d7 951;
fbcc39ad 96
b2ddf6d7 97__END__
4ab87e27 98
fbcc39ad 99=head1 METHODS
100
b5ecfcf0 101=head2 attributes
fbcc39ad 102
4ab87e27 103The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 104and so on. This determines how the action is dispatched to.
4ab87e27 105
b5ecfcf0 106=head2 class
b96f127f 107
b2ddf6d7 108Returns the class name where this action is defined.
4ab87e27 109
b5ecfcf0 110=head2 code
11bd4e3e 111
b2ddf6d7 112Returns a code reference to this action.
4ab87e27 113
b8f669f3 114=head2 dispatch( $c )
4ab87e27 115
18a9655c 116Dispatch this action against a context.
fbcc39ad 117
b8f669f3 118=head2 execute( $controller, $c, @args )
119
120Execute this action's coderef against a given controller with a given
121context and arguments
122
649fd1fa 123=head2 match( $c )
4ab87e27 124
649fd1fa 125Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 126Always returns true if Args is omitted.
4082e678 127
91955398 128=head2 compare
129
cbe555e8 130Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
131having the highest precedence.
91955398 132
b5ecfcf0 133=head2 namespace
fbcc39ad 134
4ab87e27 135Returns the private namespace this action lives in.
136
b5ecfcf0 137=head2 reverse
6b239949 138
4ab87e27 139Returns the private path for this action.
140
009b5b23 141=head2 private_path
142
143Returns absolute private path for this action. Unlike C<reverse>, the
144C<private_path> of an action is always suitable for passing to C<forward>.
145
b5ecfcf0 146=head2 name
fbcc39ad 147
18a9655c 148Returns the sub name of this action.
4ab87e27 149
059c085b 150=head2 meta
151
18a9655c 152Provided by Moose.
059c085b 153
2f381252 154=head1 AUTHORS
fbcc39ad 155
2f381252 156Catalyst Contributors, see Catalyst.pm
fbcc39ad 157
158=head1 COPYRIGHT
159
536bee89 160This library is free software. You can redistribute it and/or modify it under
fbcc39ad 161the same terms as Perl itself.
162
85d9fce6 163=cut