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