remove Action::sort_order
[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
41 'cmp' => 'compare',
42 '<=>' => 'compare',
43
2055d9ad 44 # Make general $stuff still work
45 fallback => 1,
46
47);
48
49
50
059c085b 51no warnings 'recursion';
52
53#__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
fbcc39ad 54
b2ddf6d7 55sub dispatch { # Execute ourselves against a context
56 my ( $self, $c ) = @_;
049f82e2 57 return $c->execute( $self->class, $self );
b2ddf6d7 58}
fbcc39ad 59
b2ddf6d7 60sub execute {
61 my $self = shift;
059c085b 62 $self->code->(@_);
b2ddf6d7 63}
fbcc39ad 64
b2ddf6d7 65sub match {
66 my ( $self, $c ) = @_;
e5ecd5bc 67 #would it be unreasonable to store the number of arguments
43c58153 68 #the action has as its own attribute?
e5ecd5bc 69 #it would basically eliminate the code below. ehhh. small fish
b2ddf6d7 70 return 1 unless exists $self->attributes->{Args};
71 my $args = $self->attributes->{Args}[0];
72 return 1 unless defined($args) && length($args);
73 return scalar( @{ $c->req->args } ) == $args;
74}
fbcc39ad 75
05b47f2e 76sub compare {
77 my ($a1, $a2) = @_;
78
cbe555e8 79 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
80 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
81
82 $_ = looks_like_number($_) ? $_ : ~0
83 for $a1_args, $a2_args;
84
85 return $a1_args <=> $a2_args;
05b47f2e 86}
87
e5ecd5bc 88__PACKAGE__->meta->make_immutable;
89
b2ddf6d7 901;
fbcc39ad 91
b2ddf6d7 92__END__
4ab87e27 93
fbcc39ad 94=head1 METHODS
95
b5ecfcf0 96=head2 attributes
fbcc39ad 97
4ab87e27 98The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 99and so on. This determines how the action is dispatched to.
4ab87e27 100
b5ecfcf0 101=head2 class
b96f127f 102
b2ddf6d7 103Returns the class name where this action is defined.
4ab87e27 104
b5ecfcf0 105=head2 code
11bd4e3e 106
b2ddf6d7 107Returns a code reference to this action.
4ab87e27 108
b8f669f3 109=head2 dispatch( $c )
4ab87e27 110
b8f669f3 111Dispatch this action against a context
fbcc39ad 112
b8f669f3 113=head2 execute( $controller, $c, @args )
114
115Execute this action's coderef against a given controller with a given
116context and arguments
117
649fd1fa 118=head2 match( $c )
4ab87e27 119
649fd1fa 120Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 121Always returns true if Args is omitted.
4082e678 122
91955398 123=head2 compare
124
cbe555e8 125Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
126having the highest precedence.
91955398 127
b5ecfcf0 128=head2 namespace
fbcc39ad 129
4ab87e27 130Returns the private namespace this action lives in.
131
b5ecfcf0 132=head2 reverse
6b239949 133
4ab87e27 134Returns the private path for this action.
135
b5ecfcf0 136=head2 name
fbcc39ad 137
4ab87e27 138returns the sub name of this action.
139
059c085b 140=head2 meta
141
142Provided by Moose
143
2f381252 144=head1 AUTHORS
fbcc39ad 145
2f381252 146Catalyst Contributors, see Catalyst.pm
fbcc39ad 147
148=head1 COPYRIGHT
149
536bee89 150This library is free software. You can redistribute it and/or modify it under
fbcc39ad 151the same terms as Perl itself.
152
85d9fce6 153=cut