Permalink
Browse files

[Routes] remove for now

  • Loading branch information...
1 parent 886dde7 commit 4563f298ba1059fd560a15905f2d06867209030c @masak committed Nov 29, 2010
View
@@ -1,56 +0,0 @@
-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;
-
-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) {
- # 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}."$:action"(| @_, | %_) }, named-args => %_ );
-}
-
-# RAKUDO: Ambiguous dispatch [perl #64922]
-# workaround:
-multi method dispatch (@chunks) { self.dispatch(@chunks, Hash.new) }
-
-multi method dispatch (@chunks, $env) {
-#multi method dispatch (@chunks, $env?) {
-
- my @matched = @!routes.grep: { .match(@chunks) };
- # my @matched = @!routes».match; # yay?!
-
- if @matched {
- my $result = @matched[*-1].apply($env);
- .clear for @!routes;
- return $result;
- }
- elsif defined $.default {
- $.default();
- }
- else {
- return Failure;
- }
-}
-
-multi method dispatch ($env) {
- # Do not find this .path-chunks in Rack request object,
- # but I hope we will add something like this worked like .chunks in URI.pm
- self.dispatch($env.path-chunks, $env);
-}
-# vim:ft=perl6
View
@@ -1,89 +0,0 @@
-class Routes::Route;
-has @.pattern;
-
-has Code $.code;
-
-has @.positional-args;
-has %.named-args;
-
-has $.controller = 'Root';
-has $.action = 'index';
-
-has $.slurp is rw = False;
-
-method match (@chunks) {
- # RAKUDO: Whatever do not work as specified with zip operator, [perl #64474]
- # S03: "...short list may always be extended arbitrarily by putting C<*>
- # after the final value, which replicates the final value as many times as necessary
- # so, imitation:
-
- my @tmp_pattern = @!pattern;
-
- if $!slurp and @!pattern < @chunks and @!pattern[*-1] ~~ Whatever {
- @tmp_pattern.push: * xx @chunks - @!pattern;
- } else {
- return False if @chunks != @!pattern;
- }
-
- #say 'pattern:' ~ @tmp_pattern.perl;
-
- for @chunks Z @tmp_pattern -> $chunk, Object $rule {
- if ~$chunk ~~ ($rule ~~ Pair ?? $rule.value !! $rule) {
- given $rule {
- when Regex | Whatever { @!positional-args.push($/ || $chunk) }
- when Pair { %!named-args{$rule.key} = $/ || $chunk }
- }
- }
- else {
- self.clear;
- return False;
- }
- }
- return True;
-}
-
-method apply ($env is rw) {
- given %!named-args {
- .<controller> //= $!controller;
- .<action> //= $!action;
-
- # RAKUDO: rt?
- #.<controller action>.=map: *.lc.ucfirst;
- .<controller action>.=map: *.lc;
- .<controller action>.=map: *.ucfirst;
-
- # See POST param first because HTML4 does not support PUT and DELETE
- #.<method> = $env ~~ Web::Request ?? ($env.POST<request_method> || $env<request_method>) !! 'GET';
- #.<method> .= uc;
-
- #.<env> := $env;
- #.<controllers> := %*controllers; # hm
- }
-
- # add body as last positional args if it true, this make MMD easier
- @!positional-args.push($env<body>) if $env<body>;
-
- say 'call: (|' ~ @!positional-args ~ ', |' ~ %!named-args.perl ~ ')';
-
- # Rakudo bug? It is die here with:
- # ok 3 - .add adds only complete Route objects
- # call: (|, |{"controller" => "Root", "action" => "Index"})
- # FixedIntegerArray: index out of bounds!
- # in method Routes::Route::apply (t/routes/01-basics.t:14)
- # called from method Routes::dispatch (./lib/Routes.pm:39)
- # called from method Routes::dispatch (./lib/Routes.pm:30)
- # called from Main (t/routes/01-basics.t:19)
-
- $!code(| @!positional-args, | %!named-args );
-}
-
-method is_complete {
- return ?( @!pattern && $!code );
-}
-
-method clear {
- @!positional-args = ();
- %!named-args = ();
-}
-
-# vim:ft=perl6
View
@@ -1,43 +0,0 @@
-use v6;
-
-use Test;
-plan 9;
-
-use Routes;
-ok 1, 'We use Routes and we are still alive';
-
-use Routes::Route;
-ok 1, 'We use Routes::Route and we are still alive';
-
-my $r = Routes.new;
-
-dies_ok { $r.add: Routes::Route.new },
- '.add adds only complete Route objects';
-
-$r.add: Routes::Route.new( pattern => '', code => { "Krevedko" } );
-
-is $r.dispatch(['']),
- 'Krevedko',
- "Root pattern [''] works";
-
-ok $r.add( ['foo', 'bar'], { "Yay" } ),
- '.add(@pattern, $code) -- shortcut for adding a Route object';
-
-nok $r.dispatch(['foo']),
- 'Routes returns False if it can\'t find matched Route and does not have a default';
-
-is $r.dispatch(['foo', 'bar']),
- "Yay",
- "Dispatch to Route ['foo', 'bar'])";
-
-$r.default = { "Woow" };
-
-is $r.dispatch(['foo', 'bar', 'baz']),
- "Woow",
- 'Dispatch to default, when have no matched Route';
-
-is $r.dispatch(['foo', 'bar']),
- 'Yay',
- "Dispatch ['foo', 'bar'] again";
-
-# vim:ft=perl6
@@ -1,56 +0,0 @@
-use v6;
-use Test;
-plan 9;
-
-use Routes;
-
-given my $r = Routes.new: default => { 'default' } {
- .add: [*,], { $^a }; # [@ *] or [list *] works too, but not [*]
- .add: [*,*], { $^a + $^b };
- .add: ['foo', *], { 'foo/' ~ $^a };
- .add: ['foo', *, *], { 'foo:' ~ $^a - $^b };
- .add: ['foo', *, 'bar'], { $^b };
- .add: ['bar', *], :slurp, {
- # RAKUDO: lose first arg in @_ [perl #63974]
- @_.unshift: $^a;
- @_.join: '/';
- };
-}
-
-is $r.dispatch([42]),
- 42,
- 'Pattern *';
-
-is $r.dispatch([1, 2]),
- 3,
- 'Pattern */* ';
-
-is $r.dispatch(['foo', '5']),
- 'foo/5',
- 'Pattern foo/*';
-
-is $r.dispatch(['foo', '5', 1]),
- "foo:4",
- 'Pattern foo/*/*';
-
-is $r.dispatch(['foo', 'baz', 'bar']),
- 'baz',
- 'Pattern foo/*/bar';
-
-is $r.dispatch(['bar', 1]),
- '1',
- 'Whatever with slurp take any number of args, 1 args';
-
-is $r.dispatch(['bar', 1, 2]),
- '1/2',
- 'Whatever with slurp take any numebr of args, 2 args';
-
-is $r.dispatch(['bar', 1, 2, 3]),
- '1/2/3',
- 'Whatever with slurp take any numebr of args, 3 args';
-
-is $r.dispatch(['bar', 1, 'a', 3, 'b']),
- '1/a/3/b',
- 'Whatever with slurp take any numebr of args, 4 args';
-
-# vim:ft=perl6
View
@@ -1,50 +0,0 @@
-use v6;
-
-use Test;
-plan 8;
-
-use Routes;
-given my $r = Routes.new {
- .add: ['foo', /'bar' | 'baz'/], { $^variant };
- .add: ['foo', /^ \d+ $/], { $^d };
- .add: [/^ \w+ $/], { "Yep!" if $^w ~~ Match; };
- .add: ['foo', / \d+ /], { $^d + 10 };
- .add: ['foo', / \d+ /, 'bar' ], { $^d + 1 };
- .add: ['sum', / \d+ /, / \d+ / ], { $^a + $^b };
- .add: ['bar', / $<w>=\w+ $<d>=\d+ / ], { my $m = $^a; $m<d> ~ $m<w> };
-}
-
-is $r.dispatch(<foo bar>),
- 'bar',
- 'Use regexp-rule to catch variant: bar';
-
-is $r.dispatch(<foo baz>),
- 'baz',
- 'Use regexp-rule to catch variant: baz';
-
-is $r.dispatch(['foo']),
- 'Yep!',
- "Pattern with regex \w+, put Match in args";
-
-is $r.dispatch(['foo', '50']),
- '60',
- "Dispatch ['foo', '50'] to last matched Route";
-
-is $r.dispatch(['foo', 'a50z']),
- '60',
- 'Pattern with regex \d, put Match in args';
-
-is $r.dispatch(['foo', 'item4', 'bar']),
- '5',
- 'Pattern with regexp in the middle (foo/\d+/bar)';
-
-is $r.dispatch(['sum', '2', '3']),
- '5',
- 'Pattern with two regexs';
-
-is $r.dispatch(['bar', 'item2']),
- '2item',
- 'Pattern with regexp, use abstract object in code';
-
-
-# vim:ft=perl6
@@ -1,28 +0,0 @@
-use v6;
-
-use Test;
-plan 4;
-
-use Routes;
-given my $r = Routes.new {
- .add: ['foo'|'bar'], { 'First' };
- .add: ['foo', 'a'|'b'], { 'Second' };
-}
-
-is $r.dispatch(['foo']),
- 'First',
- 'Pattern with Junction (foo|bar) foo';
-
-is $r.dispatch(['bar']),
- 'First',
- 'Pattern with Junction (foo|bar) bar';
-
-is $r.dispatch(['foo', 'a']),
- 'Second',
- 'Pattern with Junction (foo/a|b) foo/a';
-
-is $r.dispatch(['foo', 'b']),
- 'Second',
- 'Pattern with Junction (foo/a|b) foo/b';
-
-# vim:ft=perl6
View
@@ -1,29 +0,0 @@
-use v6;
-
-use Test;
-plan 4;
-
-use Routes;
-given my $r = Routes.new {
- .add: [:controller, :action ], { 'c:' ~ $:controller ~ ' a:' ~ $:action };
- .add: [:controller, / \d / ], { $:controller ~ '/' ~ $^a };
- .add: [:controller, *, * ], {
- my $c = $:controller;
- use "$c";
- is $^a, $^b, 'Test within Route code block'
- };
-}
-
-is $r.dispatch(['one', 5]),
- 'one/5',
- 'Pattern set controller';
-
-is $r.dispatch(['one', 'two']),
- 'c:one a:two',
- 'Pair rule set controller and action';
-
-is $r.dispatch(['Test', 3, 3]),
- 1,
- 'Pair set controller -- Test, code use args to make next test';
-
-# vim:ft=perl6
View
@@ -1,37 +0,0 @@
-use v6;
-
-use Test;
-plan 3;
-
-class C::Root {
- method foo { 'foo: ' ~ $^d }
- method index { 'index' }
-}
-
-class C::User {
- method show { 'show user'}
- method index { 'index of users' }
-}
-
-
-use Routes;
-given my $r = Routes.new( controllers => {Root => C::Root.new, User => C::User.new} ) {
- .connect: [:controller, :action];
- .connect: [:controller];
- .connect: [:action, /^ \d $/];
-}
-
-is $r.dispatch(['foo', 3]),
- 'foo: 3',
- 'Connect call action on the default (Root) controller';
-
-is $r.dispatch(['user', 'show']),
- 'show user',
- 'Connect to selected controller and action';
-
-# TOFIX: %!argh cleared by .clear now, and this is remove defaults.
-is $r.dispatch(['user']),
- 'index user',
- 'Connect to selected controller, call default(index) action';
-
-# vim:ft=perl6

0 comments on commit 4563f29

Please sign in to comment.