added tests for ignorance against modifier signatures
[catagits/CatalystX-Declare.git] / t / lib / TestApp / Controller / Foo.pm
CommitLineData
9c11a562 1use CatalystX::Declare;
918fb36e 2
a1dd1788 3
856ac9a7 4controller 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 }
eb97acbb 139
140 action param (Int $x) {
141 $ctx->stash(param_x => $x);
142 }
143
144 under param {
145
146 final action road (Int $y) {
147 $ctx->response->body($ctx->stash->{param_x} + $y);
148 }
149 }
e10b92dd 150 }
151 }
152 }
153
a1dd1788 154
155 #
156 # action roles
157 #
158
159 action with_role_yes
160 is final
161 as with_role
162 under base
163 with MyActionYes
164 { $ctx->res->body('YES') };
165
166 action with_role_no
167 is final
168 as with_role
169 under base
170 with MyActionNo
171 { $ctx->res->body('NO') };
172
173
174 #
175 # action classes
176 #
177
178 action book (Str $title) under base {
179 $ctx->stash(title => $title);
180 }
181
182 action view (Str $format, Int :$page) under book isa Page is final {
183 $ctx->response->body(
184 sprintf 'Page %d of "%s" as %s',
185 $page,
186 $ctx->stash->{title},
187 uc($format),
188 );
189 }
2dde75e7 190
191
192 #
193 # using final as syntax element
194 #
195
196 action final_base as 'finals' under base;
197
198 final action in_front under final_base { $ctx->response->body($ctx->action->reverse) }
199
200 under final_base, final action final_middle { $ctx->response->body($ctx->action->reverse) }
201
202 action final_at_end, final under final_base { $ctx->response->body($ctx->action->reverse) }
aae7ad1f 203
204
205 #
206 # privates
207 #
208
209 action not_really_here is private { $ctx->stash(foo => 23) }
210
211 action expose_not_really_here under base is final {
212 $ctx->forward('not_really_here');
213 $ctx->response->body($ctx->stash->{foo});
214 }
64baeca0 215
216
217 #
218 # chain target specified via action
219 #
220
221 action pointed <- base ($what) is final { $ctx->response->body("Your $what is pointed!") }
856ac9a7 222
223
224 #
225 # targets for action modifiers
226 #
227
228 action modifier_target under base is final { $ctx->response->body($ctx->action->reverse) }
229
230 action surrounded_target under base is final {
231 $ctx->response->body(join ' ', $ctx->action->reverse, $ctx->response->body || ());
232 }
233
234
235 #
236 # inline classes
237 #
238
239 final action inline_class under base {
240 $ctx->response->body( TestApp::Controller::Foo::Messenger->new(message => 'Hello')->format );
241 }
5fb5cef1 242
243
244 #
245 # validation test
246 #
247
248 final action wants_integer (Int $x) under base {
249 $ctx->response->body($x);
250 }
392e5076 251
918fb36e 252}
253