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