Commit | Line | Data |
fbcc39ad |
1 | package Catalyst::Action; |
2 | |
b2ddf6d7 |
3 | =head1 NAME |
4 | |
5 | Catalyst::Action - Catalyst Action |
6 | |
7 | =head1 SYNOPSIS |
8 | |
85d9fce6 |
9 | <form action="[%c.uri_for(c.action.reverse)%]"> |
10 | |
b2ddf6d7 |
11 | =head1 DESCRIPTION |
12 | |
e63bdf38 |
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 | |
22 | has class => (is => 'rw'); |
23 | has namespace => (is => 'rw'); |
24 | has 'reverse' => (is => 'rw'); |
25 | has attributes => (is => 'rw'); |
26 | has name => (is => 'rw'); |
27 | has code => (is => 'rw'); |
28 | |
29 | no Moose; |
30 | |
31 | no warnings 'recursion'; |
32 | |
33 | #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/); |
fbcc39ad |
34 | |
35 | use overload ( |
36 | |
37 | # Stringify to reverse for debug output etc. |
059c085b |
38 | q{""} => sub { shift->reverse() }, |
fbcc39ad |
39 | |
b8f669f3 |
40 | # Codulate to execute to invoke the encapsulated action coderef |
41 | '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; }, |
fbcc39ad |
42 | |
6a064886 |
43 | # Make general $stuff still work |
44 | fallback => 1, |
45 | |
fbcc39ad |
46 | ); |
47 | |
b2ddf6d7 |
48 | sub dispatch { # Execute ourselves against a context |
49 | my ( $self, $c ) = @_; |
e63bdf38 |
50 | #Moose todo: grrrrrr. this is no good. i don't know enough about it to |
51 | # debug it though. why can't we just call the accessor? |
4c9db64f |
52 | local $c->{namespace} = $self->namespace; |
b2ddf6d7 |
53 | return $c->execute( $self->class, $self ); |
e63bdf38 |
54 | |
55 | #believed to be equivalent: |
56 | #my $orig = $c->namespace; |
57 | #$c->namespace($self->namespace); |
58 | #my $ret = $c->execute( $self->class, $self ); |
59 | #$c->namespace($orig); |
60 | #return $ret; |
b2ddf6d7 |
61 | } |
fbcc39ad |
62 | |
b2ddf6d7 |
63 | sub execute { |
64 | my $self = shift; |
059c085b |
65 | $self->code->(@_); |
b2ddf6d7 |
66 | } |
fbcc39ad |
67 | |
b2ddf6d7 |
68 | sub match { |
69 | my ( $self, $c ) = @_; |
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 | |
b2ddf6d7 |
76 | 1; |
fbcc39ad |
77 | |
b2ddf6d7 |
78 | __END__ |
4ab87e27 |
79 | |
fbcc39ad |
80 | =head1 METHODS |
81 | |
b5ecfcf0 |
82 | =head2 attributes |
fbcc39ad |
83 | |
4ab87e27 |
84 | The sub attributes that are set for this action, like Local, Path, Private |
b2ddf6d7 |
85 | and so on. This determines how the action is dispatched to. |
4ab87e27 |
86 | |
b5ecfcf0 |
87 | =head2 class |
b96f127f |
88 | |
b2ddf6d7 |
89 | Returns the class name where this action is defined. |
4ab87e27 |
90 | |
b5ecfcf0 |
91 | =head2 code |
11bd4e3e |
92 | |
b2ddf6d7 |
93 | Returns a code reference to this action. |
4ab87e27 |
94 | |
b8f669f3 |
95 | =head2 dispatch( $c ) |
4ab87e27 |
96 | |
b8f669f3 |
97 | Dispatch this action against a context |
fbcc39ad |
98 | |
b8f669f3 |
99 | =head2 execute( $controller, $c, @args ) |
100 | |
101 | Execute this action's coderef against a given controller with a given |
102 | context and arguments |
103 | |
649fd1fa |
104 | =head2 match( $c ) |
4ab87e27 |
105 | |
649fd1fa |
106 | Check Args attribute, and makes sure number of args matches the setting. |
b2ddf6d7 |
107 | Always returns true if Args is omitted. |
4082e678 |
108 | |
b5ecfcf0 |
109 | =head2 namespace |
fbcc39ad |
110 | |
4ab87e27 |
111 | Returns the private namespace this action lives in. |
112 | |
b5ecfcf0 |
113 | =head2 reverse |
6b239949 |
114 | |
4ab87e27 |
115 | Returns the private path for this action. |
116 | |
b5ecfcf0 |
117 | =head2 name |
fbcc39ad |
118 | |
4ab87e27 |
119 | returns the sub name of this action. |
120 | |
059c085b |
121 | =head2 meta |
122 | |
123 | Provided by Moose |
124 | |
fbcc39ad |
125 | =head1 AUTHOR |
126 | |
127 | Matt S. Trout |
128 | |
129 | =head1 COPYRIGHT |
130 | |
131 | This program is free software, you can redistribute it and/or modify it under |
132 | the same terms as Perl itself. |
133 | |
85d9fce6 |
134 | =cut |