| 11 | | $router->define(sub { |
| 12 | | while (my $block = next_block()) { |
| 13 | | $_->match($block->path, $block->conditions)->to($block->params); |
| 14 | | } |
| | 7 | |
| | 8 | # simple |
| | 9 | $router->reset->define(sub { |
| | 10 | $_->match('/')->to(); |
| | 11 | $_->match('/home', { method => 'GET' })->to(); |
| | 12 | $_->match('/archives/{year}', { year => qr/^\d{4}$/ })->to(); |
| | 13 | $_->match('/users/{user_id}', { user_id => qr/^\d+$/, method => 'GET' })->to(); |
| 17 | | my @routes = $router->routes; |
| 18 | | is scalar(@routes) => blocks(), 'number'; |
| 19 | | |
| 20 | | my %params = map { $_->path, $_->params } @routes; |
| 21 | | is_deeply \%params |
| 22 | | => { map { $_->path, $_->params } blocks() }, 'path, params'; |
| 23 | | |
| 24 | | __END__ |
| 25 | | === path |
| 26 | | --- path : / |
| 27 | | --- params: { controller => 'Root', action => 'index' } |
| 28 | | |
| 29 | | === path conditions |
| 30 | | --- path : /account/login |
| 31 | | --- params : { controller => 'Account', action => 'login' } |
| 32 | | --- conditions: { method => ['GET', 'POST'] } |
| 33 | | |
| 34 | | === captures |
| 35 | | --- path : /archives/{year} |
| 36 | | --- params: { controller => 'Archive', action => 'by_year' } |
| 37 | | |
| 38 | | === captures with validation |
| 39 | | --- path : /archives/{year}/{month} |
| 40 | | --- params : { controller => 'Archive', action => 'by_month' } |
| 41 | | --- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } |
| 42 | | |
| 43 | | === captures with conditions |
| 44 | | --- path : /users/{user_id} |
| 45 | | --- params : { controller => 'User', action => 'show' } |
| 46 | | --- conditions: { user_id => qr/^\d+$/, method => 'GET' } |
| | 21 | # nest |
| | 22 | $router->reset->define(sub { |
| | 23 | $_->match('/account', sub { |
| | 24 | $_->match('/login')->to; |
| | 25 | $_->match('/logout')->to; |
| | 26 | }); |
| | 27 | }); |
| | 28 | is scalar @{[ $router->routes ]} => 2; |
| | 29 | ok grep { $_->path eq '/account/login' } $router->routes; |
| | 30 | ok grep { $_->path eq '/account/logout' } $router->routes; |