Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 67 lines (53 sloc) 1.984 kb
b1668fc ups :)
Ilya Belikin authored
1 class Routes;
2 use Routes::Route;
3
4 has @.routes;
2ae3140 Merge branch 'master' of git@github.com:masak/web
Ilya Belikin authored
5
6 # RAKUDO: invoke() not implemented in class 'Perl6Role' on 01-6 test
7 # TODO: find out is it rakudobug or not
8 #has Callable $.default is rw;
9
b1668fc ups :)
Ilya Belikin authored
10 has $.default is rw;
11
b8a7718 Routes: make .connect works, add one test for it, need much more
Ilya Belikin authored
12 has %.controllers;
13
b1668fc ups :)
Ilya Belikin authored
14 multi method add (Routes::Route $route) {
15 die "Only complete routes allowed" unless $route.?is_complete;
16 @!routes.push($route);
17 }
18
bc6b90b Routes: use shiny | %args (Tene++)
Ilya Belikin authored
19 multi method add (@pattern, Code $code) {
2af4d90 @masak [Routes] rw review; various small changes
authored
20 @!routes.push: Routes::Route.new( pattern => @pattern, code => $code, |%_);
b1668fc ups :)
Ilya Belikin authored
21 }
22
23 method connect (@pattern, *%_ is rw) {
24 %_<controller> //= 'Root';
25 %_<action> //= 'index';
2ae3140 Merge branch 'master' of git@github.com:masak/web
Ilya Belikin authored
26 # RAKUDO: die with 'Class P6protoobject already registered!' if this just in argh
b8a7718 Routes: make .connect works, add one test for it, need much more
Ilya Belikin authored
27 #%_<code> //= { %!controllers{$:controller}."$:action"(| @_, | %_) };
2f60a93 Routes: add tests for .connect, fix it default code, find bug
Ilya Belikin authored
28 say 'connect:' ~ @pattern.perl ~ ' ' ~ %_.perl;
29 @!routes.push: Routes::Route.new( pattern => @pattern, code => { %!controllers{$:controller.ucfirst}."$:action"(| @_, | %_) }, argh => %_ );
b1668fc ups :)
Ilya Belikin authored
30 }
31
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
32 # I think it should work as I mean without this one
33 multi method dispatch (@chunks) { self.dispatch(@chunks, Hash.new) }
34
35 multi method dispatch (@chunks, %param) {
36 #multi method dispatch (@chunks, %param?) {
b1668fc ups :)
Ilya Belikin authored
37 my @matched = @!routes.grep: { .match(@chunks) };
38
39 if @matched {
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
40 my $result = @matched[*-1].apply(%param);
b1668fc ups :)
Ilya Belikin authored
41 .clear for @!routes;
42 return $result;
43 }
44 elsif defined $.default {
45 $.default();
46 }
47 else {
48 return Failure;
49 }
50 }
51
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
52 # draft
53 multi method dispatch ($request) {
2af4d90 @masak [Routes] rw review; various small changes
authored
54 my %params;
55 %params<request> = $request;
56 %params<post> = $request.POST;
57 %params<get> = $request.GET;
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
58
2af4d90 @masak [Routes] rw review; various small changes
authored
59 # Use param as method first because HTML4 does not support PUT and DELETE
60 %params<method> = %params<post><request_method> || $request.request_method;
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
61
62 # Do not find this .path-chunks in rack request object,
63 # but I hope we will add something like this with chunks from URI.pm
2af4d90 @masak [Routes] rw review; various small changes
authored
64 self.dispatch($request.path-chunks, %params);
d10c450 Routes: add some draft .dispatch($request) method, addd %param to
Ilya Belikin authored
65 }
b1668fc ups :)
Ilya Belikin authored
66 # vim:ft=perl6
Something went wrong with that request. Please try again.