Commit | Line | Data |
fbcc39ad |
1 | package Catalyst::Action; |
2 | |
3 | use strict; |
4 | use base qw/Class::Accessor::Fast/; |
5 | |
11bd4e3e |
6 | __PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/); |
fbcc39ad |
7 | |
8 | use overload ( |
9 | |
10 | # Stringify to reverse for debug output etc. |
11 | q{""} => sub { shift->{reverse} }, |
12 | |
b8f669f3 |
13 | # Codulate to execute to invoke the encapsulated action coderef |
14 | '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; }, |
fbcc39ad |
15 | |
6a064886 |
16 | # Make general $stuff still work |
17 | fallback => 1, |
18 | |
fbcc39ad |
19 | ); |
20 | |
21 | =head1 NAME |
22 | |
23 | Catalyst::Action - Catalyst Action |
24 | |
25 | =head1 SYNOPSIS |
26 | |
27 | See L<Catalyst>. |
28 | |
29 | =head1 DESCRIPTION |
30 | |
4ab87e27 |
31 | This class represents a Catalyst Action. You can access the object for the |
32 | currently dispatched action via $c->action |
33 | |
fbcc39ad |
34 | =head1 METHODS |
35 | |
b5ecfcf0 |
36 | =head2 attributes |
fbcc39ad |
37 | |
4ab87e27 |
38 | The sub attributes that are set for this action, like Local, Path, Private |
39 | and so on. |
40 | |
b5ecfcf0 |
41 | =head2 class |
b96f127f |
42 | |
4ab87e27 |
43 | Returns the class name of this action |
44 | |
b5ecfcf0 |
45 | =head2 code |
11bd4e3e |
46 | |
4ab87e27 |
47 | Returns a code reference to this action |
48 | |
b8f669f3 |
49 | =head2 dispatch( $c ) |
4ab87e27 |
50 | |
b8f669f3 |
51 | Dispatch this action against a context |
fbcc39ad |
52 | |
53 | =cut |
54 | |
b8f669f3 |
55 | sub dispatch { # Execute ourselves against a context |
fbcc39ad |
56 | my ( $self, $c ) = @_; |
261c571e |
57 | local $c->namespace = $self->namespace; |
11bd4e3e |
58 | return $c->execute( $self->class, $self ); |
fbcc39ad |
59 | } |
60 | |
b8f669f3 |
61 | =head2 execute( $controller, $c, @args ) |
62 | |
63 | Execute this action's coderef against a given controller with a given |
64 | context and arguments |
65 | |
66 | =cut |
67 | |
68 | sub execute { |
69 | my $self = shift; |
70 | $self->{code}->(@_); |
71 | } |
72 | |
649fd1fa |
73 | =head2 match( $c ) |
4ab87e27 |
74 | |
649fd1fa |
75 | Check Args attribute, and makes sure number of args matches the setting. |
4082e678 |
76 | |
77 | =cut |
78 | |
79 | sub match { |
80 | my ( $self, $c ) = @_; |
81 | return 1 unless exists $self->attributes->{Args}; |
141459fa |
82 | my $args = $self->attributes->{Args}[0]; |
83 | return 1 unless defined($args) && length($args); |
84 | return scalar( @{ $c->req->args } ) == $args; |
4082e678 |
85 | } |
86 | |
b5ecfcf0 |
87 | =head2 namespace |
fbcc39ad |
88 | |
4ab87e27 |
89 | Returns the private namespace this action lives in. |
90 | |
b5ecfcf0 |
91 | =head2 reverse |
6b239949 |
92 | |
4ab87e27 |
93 | Returns the private path for this action. |
94 | |
b5ecfcf0 |
95 | =head2 name |
fbcc39ad |
96 | |
4ab87e27 |
97 | returns the sub name of this action. |
98 | |
fbcc39ad |
99 | =head1 AUTHOR |
100 | |
101 | Matt S. Trout |
102 | |
103 | =head1 COPYRIGHT |
104 | |
105 | This program is free software, you can redistribute it and/or modify it under |
106 | the same terms as Perl itself. |
107 | |
108 | =cut |
109 | |
110 | 1; |