Skip to content

Commit

Permalink
added args attribute to Mojolicious::Routes::Match
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 22, 2013
1 parent d0383ed commit 7d79839
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.87 2013-02-23
- Added args attribute to Mojolicious::Routes::Match.
- Improved tests.

3.86 2013-02-22
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -105,7 +105,7 @@ sub _callback {
my ($self, $c, $field, $staging) = @_;
$c->stash->{'mojo.routed'}++;
$c->app->log->debug('Routing to a callback.');
my $continue = $field->{cb}->($c);
my $continue = $field->{cb}->($c, @{$c->match->args});
return !$staging || $continue ? 1 : undef;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ sub _controller {
# Try to call action
if (my $sub = $app->can($method)) {
$c->stash->{'mojo.routed'}++ unless $staging;
$continue = $app->$sub;
$continue = $app->$sub(@{$c->match->args});
}

# Action not found
Expand Down
9 changes: 8 additions & 1 deletion lib/Mojolicious/Routes/Match.pm
@@ -1,9 +1,9 @@
package Mojolicious::Routes::Match;
use Mojo::Base -base;

has [qw(args stack)] => sub { [] };
has captures => sub { {} };
has [qw(endpoint root)];
has stack => sub { [] };

sub new {
my $self = shift->SUPER::new;
Expand Down Expand Up @@ -164,6 +164,13 @@ structures.
L<Mojolicious::Routes::Match> implements the following attributes.
=head2 args
my $args = $m->args;
$m = $m->args(['Hello World!']);
Arguments for actions.
=head2 captures
my $captures = $m->captures;
Expand Down
9 changes: 5 additions & 4 deletions t/mojolicious/group_lite_app.t
Expand Up @@ -23,20 +23,21 @@ get '/expiration' => sub {

under sub {
my $self = shift;
$self->match->args(['cool']);
return undef unless $self->req->headers->header('X-Bender');
$self->res->headers->add('X-Under' => 23);
$self->res->headers->add('X-Under' => 24);
1;
};

get '/with_under' => sub {
my $self = shift;
$self->render_text('Unders are cool!');
my ($self, $msg) = @_;
$self->render_text("Unders are $msg!");
};

get '/with_under_too' => sub {
my $self = shift;
$self->render_text('Unders are cool too!');
my ($self, $msg) = @_;
$self->render_text("Unders are $msg too!");
};

under sub {
Expand Down
7 changes: 4 additions & 3 deletions t/mojolicious/lib/MojoliciousTest/Foo.pm
Expand Up @@ -45,16 +45,17 @@ sub stage1 {
my $self = shift;

# Authenticated
return 1 if $self->req->headers->header('X-Pass');
$self->match->args([$self->some_plugin]) and return 1
if $self->req->headers->header('X-Pass');

# Fail
$self->render_text('Go away!');
return undef;
}

sub stage2 {
my $self = shift;
$self->render_text($self->some_plugin);
my ($self, $msg) = @_;
$self->render_text($msg);
}

sub syntaxerror { shift->render('syntaxerror', format => 'html') }
Expand Down

0 comments on commit 7d79839

Please sign in to comment.