added action classes and roles
[catagits/CatalystX-Declare.git] / t / lib / TestApp / Controller / Foo.pm
1 use CatalystX::Declarative;
2
3 role MyActionYes {
4     around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? $self->$orig(@args) : undef }
5 }
6
7 role TestApp::Try::Aliasing::MyActionNo {
8     around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? undef : $self->$orig(@args) }
9 }
10
11 class 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
19 controller TestApp::Controller::Foo {
20
21     use constant MyActionNo => 'TestApp::Try::Aliasing::MyActionNo';
22
23     #
24     #   look, a Moose!
25     #
26
27     has title => (
28         is      => 'ro',
29         isa     => 'Str',
30         default => 'TestApp',
31     );
32
33
34     #
35     #   normal methods are very useful too
36     #
37
38     method welcome_message { sprintf 'Welcome to %s!', $self->title }
39
40     method greet (Str $name) { "Hello, $name!" }
41
42
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
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 {
129                     $ctx->response->body($ctx->action->reverse);
130                 }
131             }
132         }
133     }
134
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     }
171 }
172