added final syntax element
[catagits/CatalystX-Declare.git] / t / lib / TestApp / Controller / Foo.pm
CommitLineData
918fb36e 1use CatalystX::Declarative;
2
a1dd1788 3role MyActionYes {
4 around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? $self->$orig(@args) : undef }
5}
6
7role TestApp::Try::Aliasing::MyActionNo {
8 around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? undef : $self->$orig(@args) }
9}
10
11class TestApp::Action::Page extends Catalyst::Action {
12
13 around execute ($controller, $ctx, @args) {
14 my $page = $ctx->request->params->{page} || 1;
15 return $self->$orig($controller, $ctx, @args, page => $page);
16 }
17}
18
918fb36e 19controller TestApp::Controller::Foo {
20
a1dd1788 21 use constant MyActionNo => 'TestApp::Try::Aliasing::MyActionNo';
918fb36e 22
a0ebba1d 23 #
24 # look, a Moose!
25 #
918fb36e 26
a0ebba1d 27 has title => (
28 is => 'ro',
29 isa => 'Str',
30 default => 'TestApp',
31 );
918fb36e 32
918fb36e 33
a0ebba1d 34 #
35 # normal methods are very useful too
36 #
918fb36e 37
a0ebba1d 38 method welcome_message { sprintf 'Welcome to %s!', $self->title }
918fb36e 39
a0ebba1d 40 method greet (Str $name) { "Hello, $name!" }
918fb36e 41
918fb36e 42
a0ebba1d 43 #
44 # the simple stuff
45 #
46
47 action base under '/base' as 'foo';
48
49 action root under base as '' is final {
50 $ctx->response->body( $self->welcome_message );
51 }
52
53
54 #
55 # with arguments
56 #
57
58 action with_args under base;
59
60 action hello (Str $name) under with_args is final {
61 $ctx->response->body($self->greet(ucfirst $name));
62 }
63
64 action at_end (Int $x, Int $y) under with_args is final {
65 $ctx->response->body( $x * $y );
66 }
67
68 action in_the_middle (Int $x, Int $y) under with_args {
69 $ctx->stash(result => $x * $y);
70 }
71 action end_of_the_middle under in_the_middle is final {
72 $ctx->response->body($ctx->stash->{result} * 2);
73 }
74
75 action all_the_way (Int $x) under with_args as '' {
76 $ctx->stash(x => $x);
77 }
78 action through_the_sky (Int $y) under all_the_way as '' {
79 $ctx->stash(y => $y);
80 }
81 action and_beyond (@rest) under through_the_sky as fhtagn is final {
82 $ctx->response->body(join ', ',
83 $ctx->stash->{x},
84 $ctx->stash->{y},
85 @rest,
86 );
87 }
88
89
90 #
91 # under is also a valid keyword
92 #
93
94 under base action under_base as under;
95
96 under under_base as '' action even_more_under (Int $i) is final {
97 $ctx->response->body("under $i");
98 }
99
100
101 #
102 # too many words? go comma go!
103 #
104
105 action comma_base, as '', under base;
106
107 under comma_base, is final, action comma ($str), as ',comma' {
108 $ctx->response->body($str);
109 }
110
e10b92dd 111
112 #
113 # subnamespacing
114 #
115
116 action lower under base;
117
118 under lower {
119
120 action down;
121
122 under down {
123
124 action the;
125
126 under the {
127
128 action stream is final {
3187b9aa 129 $ctx->response->body($ctx->action->reverse);
e10b92dd 130 }
131 }
132 }
133 }
134
a1dd1788 135
136 #
137 # action roles
138 #
139
140 action with_role_yes
141 is final
142 as with_role
143 under base
144 with MyActionYes
145 { $ctx->res->body('YES') };
146
147 action with_role_no
148 is final
149 as with_role
150 under base
151 with MyActionNo
152 { $ctx->res->body('NO') };
153
154
155 #
156 # action classes
157 #
158
159 action book (Str $title) under base {
160 $ctx->stash(title => $title);
161 }
162
163 action view (Str $format, Int :$page) under book isa Page is final {
164 $ctx->response->body(
165 sprintf 'Page %d of "%s" as %s',
166 $page,
167 $ctx->stash->{title},
168 uc($format),
169 );
170 }
2dde75e7 171
172
173 #
174 # using final as syntax element
175 #
176
177 action final_base as 'finals' under base;
178
179 final action in_front under final_base { $ctx->response->body($ctx->action->reverse) }
180
181 under final_base, final action final_middle { $ctx->response->body($ctx->action->reverse) }
182
183 action final_at_end, final under final_base { $ctx->response->body($ctx->action->reverse) }
918fb36e 184}
185