remove Action::sort_order
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
1 package Catalyst::Action;
2
3 =head1 NAME
4
5 Catalyst::Action - Catalyst Action
6
7 =head1 SYNOPSIS
8
9     <form action="[%c.uri_for(c.action)%]">
10
11 =head1 DESCRIPTION
12
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.
17
18 =cut
19
20 use Moose;
21 use Scalar::Util 'looks_like_number';
22 with 'MooseX::Emulate::Class::Accessor::Fast';
23 use namespace::clean -except => 'meta';
24
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');
31
32 use 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
40     # Which action takes precedence
41     'cmp' => 'compare',
42     '<=>' => 'compare',
43
44     # Make general $stuff still work
45     fallback => 1,
46
47 );
48
49
50
51 no warnings 'recursion';
52
53 #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
54
55 sub dispatch {    # Execute ourselves against a context
56     my ( $self, $c ) = @_;
57     return $c->execute( $self->class, $self );
58 }
59
60 sub execute {
61   my $self = shift;
62   $self->code->(@_);
63 }
64
65 sub match {
66     my ( $self, $c ) = @_;
67     #would it be unreasonable to store the number of arguments
68     #the action has as its own attribute?
69     #it would basically eliminate the code below.  ehhh. small fish
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 }
75
76 sub compare {
77     my ($a1, $a2) = @_;
78
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;
86 }
87
88 __PACKAGE__->meta->make_immutable;
89
90 1;
91
92 __END__
93
94 =head1 METHODS
95
96 =head2 attributes
97
98 The sub attributes that are set for this action, like Local, Path, Private
99 and so on. This determines how the action is dispatched to.
100
101 =head2 class
102
103 Returns the class name where this action is defined.
104
105 =head2 code
106
107 Returns a code reference to this action.
108
109 =head2 dispatch( $c )
110
111 Dispatch this action against a context
112
113 =head2 execute( $controller, $c, @args )
114
115 Execute this action's coderef against a given controller with a given
116 context and arguments
117
118 =head2 match( $c )
119
120 Check Args attribute, and makes sure number of args matches the setting.
121 Always returns true if Args is omitted.
122
123 =head2 compare
124
125 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
126 having the highest precedence.
127
128 =head2 namespace
129
130 Returns the private namespace this action lives in.
131
132 =head2 reverse
133
134 Returns the private path for this action.
135
136 =head2 name
137
138 returns the sub name of this action.
139
140 =head2 meta
141
142 Provided by Moose
143
144 =head1 AUTHORS
145
146 Catalyst Contributors, see Catalyst.pm
147
148 =head1 COPYRIGHT
149
150 This library is free software. You can redistribute it and/or modify it under
151 the same terms as Perl itself.
152
153 =cut