Commit | Line | Data |
9c11a562 |
1 | use CatalystX::Declare; |
918fb36e |
2 | |
a1dd1788 |
3 | |
856ac9a7 |
4 | controller TestApp::Controller::Foo with TestApp::TestRole { |
5 | |
6 | use constant MyActionNo => 'TestApp::Try::Aliasing::MyActionNo'; |
a1dd1788 |
7 | |
856ac9a7 |
8 | class ::Messenger { |
a1dd1788 |
9 | |
856ac9a7 |
10 | has message => (is => 'rw'); |
11 | |
12 | method format { uc $self->message } |
a1dd1788 |
13 | } |
a1dd1788 |
14 | |
856ac9a7 |
15 | role MyActionYes { |
16 | around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? $self->$orig(@args) : undef } |
17 | } |
918fb36e |
18 | |
856ac9a7 |
19 | role TestApp::Try::Aliasing::MyActionNo { |
20 | around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? undef : $self->$orig(@args) } |
21 | } |
22 | |
23 | class TestApp::Action::Page extends Catalyst::Action { |
24 | |
25 | around execute ($controller, $ctx, @args) { |
26 | my $page = $ctx->request->params->{page} || 1; |
27 | return $self->$orig($controller, $ctx, @args, page => $page); |
28 | } |
29 | } |
918fb36e |
30 | |
a0ebba1d |
31 | # |
32 | # look, a Moose! |
33 | # |
918fb36e |
34 | |
a0ebba1d |
35 | has title => ( |
36 | is => 'ro', |
37 | isa => 'Str', |
38 | default => 'TestApp', |
39 | ); |
918fb36e |
40 | |
918fb36e |
41 | |
a0ebba1d |
42 | # |
43 | # normal methods are very useful too |
44 | # |
918fb36e |
45 | |
a0ebba1d |
46 | method welcome_message { sprintf 'Welcome to %s!', $self->title } |
918fb36e |
47 | |
a0ebba1d |
48 | method greet (Str $name) { "Hello, $name!" } |
918fb36e |
49 | |
918fb36e |
50 | |
a0ebba1d |
51 | # |
52 | # the simple stuff |
53 | # |
54 | |
55 | action base under '/base' as 'foo'; |
56 | |
57 | action root under base as '' is final { |
58 | $ctx->response->body( $self->welcome_message ); |
59 | } |
60 | |
61 | |
62 | # |
63 | # with arguments |
64 | # |
65 | |
66 | action with_args under base; |
67 | |
68 | action hello (Str $name) under with_args is final { |
69 | $ctx->response->body($self->greet(ucfirst $name)); |
70 | } |
71 | |
72 | action at_end (Int $x, Int $y) under with_args is final { |
73 | $ctx->response->body( $x * $y ); |
74 | } |
75 | |
76 | action in_the_middle (Int $x, Int $y) under with_args { |
77 | $ctx->stash(result => $x * $y); |
78 | } |
79 | action end_of_the_middle under in_the_middle is final { |
80 | $ctx->response->body($ctx->stash->{result} * 2); |
81 | } |
82 | |
83 | action all_the_way (Int $x) under with_args as '' { |
84 | $ctx->stash(x => $x); |
85 | } |
86 | action through_the_sky (Int $y) under all_the_way as '' { |
87 | $ctx->stash(y => $y); |
88 | } |
89 | action and_beyond (@rest) under through_the_sky as fhtagn is final { |
90 | $ctx->response->body(join ', ', |
91 | $ctx->stash->{x}, |
92 | $ctx->stash->{y}, |
93 | @rest, |
94 | ); |
95 | } |
96 | |
97 | |
98 | # |
99 | # under is also a valid keyword |
100 | # |
101 | |
102 | under base action under_base as under; |
103 | |
104 | under under_base as '' action even_more_under (Int $i) is final { |
105 | $ctx->response->body("under $i"); |
106 | } |
107 | |
108 | |
109 | # |
110 | # too many words? go comma go! |
111 | # |
112 | |
113 | action comma_base, as '', under base; |
114 | |
115 | under comma_base, is final, action comma ($str), as ',comma' { |
116 | $ctx->response->body($str); |
117 | } |
118 | |
e10b92dd |
119 | |
120 | # |
121 | # subnamespacing |
122 | # |
123 | |
124 | action lower under base; |
125 | |
126 | under lower { |
127 | |
128 | action down; |
129 | |
130 | under down { |
131 | |
132 | action the; |
133 | |
134 | under the { |
135 | |
136 | action stream is final { |
3187b9aa |
137 | $ctx->response->body($ctx->action->reverse); |
e10b92dd |
138 | } |
139 | } |
140 | } |
141 | } |
142 | |
a1dd1788 |
143 | |
144 | # |
145 | # action roles |
146 | # |
147 | |
148 | action with_role_yes |
149 | is final |
150 | as with_role |
151 | under base |
152 | with MyActionYes |
153 | { $ctx->res->body('YES') }; |
154 | |
155 | action with_role_no |
156 | is final |
157 | as with_role |
158 | under base |
159 | with MyActionNo |
160 | { $ctx->res->body('NO') }; |
161 | |
162 | |
163 | # |
164 | # action classes |
165 | # |
166 | |
167 | action book (Str $title) under base { |
168 | $ctx->stash(title => $title); |
169 | } |
170 | |
171 | action view (Str $format, Int :$page) under book isa Page is final { |
172 | $ctx->response->body( |
173 | sprintf 'Page %d of "%s" as %s', |
174 | $page, |
175 | $ctx->stash->{title}, |
176 | uc($format), |
177 | ); |
178 | } |
2dde75e7 |
179 | |
180 | |
181 | # |
182 | # using final as syntax element |
183 | # |
184 | |
185 | action final_base as 'finals' under base; |
186 | |
187 | final action in_front under final_base { $ctx->response->body($ctx->action->reverse) } |
188 | |
189 | under final_base, final action final_middle { $ctx->response->body($ctx->action->reverse) } |
190 | |
191 | action final_at_end, final under final_base { $ctx->response->body($ctx->action->reverse) } |
aae7ad1f |
192 | |
193 | |
194 | # |
195 | # privates |
196 | # |
197 | |
198 | action not_really_here is private { $ctx->stash(foo => 23) } |
199 | |
200 | action expose_not_really_here under base is final { |
201 | $ctx->forward('not_really_here'); |
202 | $ctx->response->body($ctx->stash->{foo}); |
203 | } |
64baeca0 |
204 | |
205 | |
206 | # |
207 | # chain target specified via action |
208 | # |
209 | |
210 | action pointed <- base ($what) is final { $ctx->response->body("Your $what is pointed!") } |
856ac9a7 |
211 | |
212 | |
213 | # |
214 | # targets for action modifiers |
215 | # |
216 | |
217 | action modifier_target under base is final { $ctx->response->body($ctx->action->reverse) } |
218 | |
219 | action surrounded_target under base is final { |
220 | $ctx->response->body(join ' ', $ctx->action->reverse, $ctx->response->body || ()); |
221 | } |
222 | |
223 | |
224 | # |
225 | # inline classes |
226 | # |
227 | |
228 | final action inline_class under base { |
229 | $ctx->response->body( TestApp::Controller::Foo::Messenger->new(message => 'Hello')->format ); |
230 | } |
918fb36e |
231 | } |
232 | |