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