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 | |
ac5c933b |
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; |
21 | |
241edc9b |
22 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
23 | |
5fb12dbb |
24 | has class => (is => 'rw'); |
25 | has namespace => (is => 'rw'); |
26 | has 'reverse' => (is => 'rw'); |
27 | has attributes => (is => 'rw'); |
28 | has name => (is => 'rw'); |
29 | has code => (is => 'rw'); |
059c085b |
30 | |
31 | no Moose; |
32 | |
2055d9ad |
33 | use overload ( |
34 | |
35 | # Stringify to reverse for debug output etc. |
36 | q{""} => sub { shift->{reverse} }, |
37 | |
38 | # Codulate to execute to invoke the encapsulated action coderef |
39 | '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; }, |
40 | |
41 | # Make general $stuff still work |
42 | fallback => 1, |
43 | |
44 | ); |
45 | |
46 | |
47 | |
059c085b |
48 | no warnings 'recursion'; |
49 | |
50 | #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/); |
fbcc39ad |
51 | |
b2ddf6d7 |
52 | sub dispatch { # Execute ourselves against a context |
53 | my ( $self, $c ) = @_; |
049f82e2 |
54 | return $c->execute( $self->class, $self ); |
b2ddf6d7 |
55 | } |
fbcc39ad |
56 | |
b2ddf6d7 |
57 | sub execute { |
58 | my $self = shift; |
059c085b |
59 | $self->code->(@_); |
b2ddf6d7 |
60 | } |
fbcc39ad |
61 | |
b2ddf6d7 |
62 | sub match { |
63 | my ( $self, $c ) = @_; |
e5ecd5bc |
64 | #would it be unreasonable to store the number of arguments |
65 | #the action has as it's own attribute? |
66 | #it would basically eliminate the code below. ehhh. small fish |
b2ddf6d7 |
67 | return 1 unless exists $self->attributes->{Args}; |
68 | my $args = $self->attributes->{Args}[0]; |
69 | return 1 unless defined($args) && length($args); |
70 | return scalar( @{ $c->req->args } ) == $args; |
71 | } |
fbcc39ad |
72 | |
e5ecd5bc |
73 | __PACKAGE__->meta->make_immutable; |
74 | |
b2ddf6d7 |
75 | 1; |
fbcc39ad |
76 | |
b2ddf6d7 |
77 | __END__ |
4ab87e27 |
78 | |
fbcc39ad |
79 | =head1 METHODS |
80 | |
b5ecfcf0 |
81 | =head2 attributes |
fbcc39ad |
82 | |
4ab87e27 |
83 | The sub attributes that are set for this action, like Local, Path, Private |
b2ddf6d7 |
84 | and so on. This determines how the action is dispatched to. |
4ab87e27 |
85 | |
b5ecfcf0 |
86 | =head2 class |
b96f127f |
87 | |
b2ddf6d7 |
88 | Returns the class name where this action is defined. |
4ab87e27 |
89 | |
b5ecfcf0 |
90 | =head2 code |
11bd4e3e |
91 | |
b2ddf6d7 |
92 | Returns a code reference to this action. |
4ab87e27 |
93 | |
b8f669f3 |
94 | =head2 dispatch( $c ) |
4ab87e27 |
95 | |
b8f669f3 |
96 | Dispatch this action against a context |
fbcc39ad |
97 | |
b8f669f3 |
98 | =head2 execute( $controller, $c, @args ) |
99 | |
100 | Execute this action's coderef against a given controller with a given |
101 | context and arguments |
102 | |
649fd1fa |
103 | =head2 match( $c ) |
4ab87e27 |
104 | |
649fd1fa |
105 | Check Args attribute, and makes sure number of args matches the setting. |
b2ddf6d7 |
106 | Always returns true if Args is omitted. |
4082e678 |
107 | |
b5ecfcf0 |
108 | =head2 namespace |
fbcc39ad |
109 | |
4ab87e27 |
110 | Returns the private namespace this action lives in. |
111 | |
b5ecfcf0 |
112 | =head2 reverse |
6b239949 |
113 | |
4ab87e27 |
114 | Returns the private path for this action. |
115 | |
b5ecfcf0 |
116 | =head2 name |
fbcc39ad |
117 | |
4ab87e27 |
118 | returns the sub name of this action. |
119 | |
059c085b |
120 | =head2 meta |
121 | |
122 | Provided by Moose |
123 | |
2f381252 |
124 | =head1 AUTHORS |
fbcc39ad |
125 | |
2f381252 |
126 | Catalyst Contributors, see Catalyst.pm |
fbcc39ad |
127 | |
128 | =head1 COPYRIGHT |
129 | |
130 | This program is free software, you can redistribute it and/or modify it under |
131 | the same terms as Perl itself. |
132 | |
85d9fce6 |
133 | =cut |