Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d42596a457
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 67 lines (53 sloc) 1.984 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
class Routes;
use Routes::Route;

has @.routes;

# RAKUDO: invoke() not implemented in class 'Perl6Role' on 01-6 test
# TODO: find out is it rakudobug or not
#has Callable $.default is rw;

has $.default is rw;

has %.controllers;

multi method add (Routes::Route $route) {
    die "Only complete routes allowed" unless $route.?is_complete;
    @!routes.push($route);
}

multi method add (@pattern, Code $code) {
    @!routes.push: Routes::Route.new( pattern => @pattern, code => $code, |%_);
}

method connect (@pattern, *%_ is rw) {
    %_<controller> //= 'Root';
    %_<action> //= 'index';
    # RAKUDO: die with 'Class P6protoobject already registered!' if this just in argh
    #%_<code> //= { %!controllers{$:controller}."$:action"(| @_, | %_) };
    say 'connect:' ~ @pattern.perl ~ ' ' ~ %_.perl;
    @!routes.push: Routes::Route.new( pattern => @pattern, code => { %!controllers{$:controller.ucfirst}."$:action"(| @_, | %_) }, argh => %_ );
}

# I think it should work as I mean without this one
multi method dispatch (@chunks) { self.dispatch(@chunks, Hash.new) }

multi method dispatch (@chunks, %param) {
#multi method dispatch (@chunks, %param?) {
    my @matched = @!routes.grep: { .match(@chunks) };
    
    if @matched {
        my $result = @matched[*-1].apply(%param);
        .clear for @!routes;
        return $result;
    }
    elsif defined $.default {
        $.default();
    }
    else {
        return Failure;
    }
}

# draft
multi method dispatch ($request) {
    my %params;
    %params<request> = $request;
    %params<post> = $request.POST;
    %params<get> = $request.GET;

    # Use param as method first because HTML4 does not support PUT and DELETE
    %params<method> = %params<post><request_method> || $request.request_method;

    # Do not find this .path-chunks in rack request object,
    # but I hope we will add something like this with chunks from URI.pm
    self.dispatch($request.path-chunks, %params);
}
# vim:ft=perl6
Something went wrong with that request. Please try again.