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