nicer action sorting for Path
[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 sort_order {
77 my $self = shift;
78
79 my ($args) = @{ $self->attributes->{Args} || [] };
80
81 return $args if looks_like_number($args);
82
83 return ~0;
84}
85
86sub compare {
87 my ($a1, $a2) = @_;
88
89 return $a1->sort_order <=> $a2->sort_order;
90}
91
e5ecd5bc 92__PACKAGE__->meta->make_immutable;
93
b2ddf6d7 941;
fbcc39ad 95
b2ddf6d7 96__END__
4ab87e27 97
fbcc39ad 98=head1 METHODS
99
b5ecfcf0 100=head2 attributes
fbcc39ad 101
4ab87e27 102The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 103and so on. This determines how the action is dispatched to.
4ab87e27 104
b5ecfcf0 105=head2 class
b96f127f 106
b2ddf6d7 107Returns the class name where this action is defined.
4ab87e27 108
b5ecfcf0 109=head2 code
11bd4e3e 110
b2ddf6d7 111Returns a code reference to this action.
4ab87e27 112
b8f669f3 113=head2 dispatch( $c )
4ab87e27 114
b8f669f3 115Dispatch this action against a context
fbcc39ad 116
b8f669f3 117=head2 execute( $controller, $c, @args )
118
119Execute this action's coderef against a given controller with a given
120context and arguments
121
649fd1fa 122=head2 match( $c )
4ab87e27 123
649fd1fa 124Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 125Always returns true if Args is omitted.
4082e678 126
b5ecfcf0 127=head2 namespace
fbcc39ad 128
4ab87e27 129Returns the private namespace this action lives in.
130
b5ecfcf0 131=head2 reverse
6b239949 132
4ab87e27 133Returns the private path for this action.
134
b5ecfcf0 135=head2 name
fbcc39ad 136
4ab87e27 137returns the sub name of this action.
138
059c085b 139=head2 meta
140
141Provided by Moose
142
2f381252 143=head1 AUTHORS
fbcc39ad 144
2f381252 145Catalyst Contributors, see Catalyst.pm
fbcc39ad 146
147=head1 COPYRIGHT
148
536bee89 149This library is free software. You can redistribute it and/or modify it under
fbcc39ad 150the same terms as Perl itself.
151
85d9fce6 152=cut