Skip to content

Commit

Permalink
added a few more route examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 28, 2012
1 parent a5b9bc3 commit 125ae67
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
3 changes: 1 addition & 2 deletions lib/Mojolicious/Command/test.pm
Expand Up @@ -38,8 +38,7 @@ sub run {

# List test files
my $home = Mojo::Home->new($path);
$_ =~ /\.t$/ and push(@tests, $home->rel_file($_))
for @{$home->list_files};
/\.t$/ and push(@tests, $home->rel_file($_)) for @{$home->list_files};

$path = realpath $path;
say "Running tests from '$path'.";
Expand Down
14 changes: 8 additions & 6 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -76,14 +76,16 @@ EOF
sub get_data_template {
my ($self, $options, $template) = @_;

# Index and find DATA templates
# Index DATA templates
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{Mojo::Command->get_all_data($class)};
}
}
return Mojo::Command->new->get_data($template, $self->{index}->{$template});

# Find template
return Mojo::Command->get_data($template, $self->{index}->{$template});
}

# "Bodies are for hookers and fat people."
Expand Down Expand Up @@ -217,16 +219,16 @@ sub _detect_handler {
# Templates
return unless my $file = $self->template_name($options);
unless ($self->{templates}) {
$_ =~ s/\.(\w+)$// and $self->{templates}->{$_} ||= $1
s/\.(\w+)$// and $self->{templates}->{$_} ||= $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths};
}
return $self->{templates}->{$file} if exists $self->{templates}->{$file};

# DATA templates
unless ($self->{data}) {
$_ =~ s/\.(\w+)$// and $self->{data}->{$_} ||= $1
for map { sort keys %{Mojo::Command->get_all_data($_)} }
@{$self->classes};
my @templates =
map { sort keys %{Mojo::Command->get_all_data($_)} } @{$self->classes};
s/\.(\w+)$// and $self->{data}->{$_} ||= $1 for @templates;
}
return $self->{data}->{$file} if exists $self->{data}->{$file};

Expand Down
16 changes: 8 additions & 8 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -166,7 +166,6 @@ sub route {

sub to {
my $self = shift;
return $self unless @_;

# Single argument
my ($shortcut, $defaults);
Expand All @@ -181,10 +180,7 @@ sub to {
else {

# Odd
if (@_ % 2) {
$shortcut = shift;
$defaults = {@_};
}
if (@_ % 2) { ($shortcut, $defaults) = (shift, {@_}) }

# Even
else {
Expand Down Expand Up @@ -214,8 +210,7 @@ sub to {

# Defaults
my $pattern = $self->pattern;
my $old = $pattern->defaults;
$pattern->defaults({%$old, %$defaults}) if $defaults;
$pattern->defaults({%{$pattern->defaults}, %$defaults}) if $defaults;

return $self;
}
Expand Down Expand Up @@ -487,6 +482,8 @@ L<Mojolicious::Lite> tutorial for more argument variations.
Activate conditions for this route. Note that this automatically disables the
routing cache, since conditions are too complex for caching.
$r->route('/foo')->over(host => qr/mojolicio\.us/)->to('foo#bar');
=head2 C<parse>
$r = $r->parse('/:controller/:action');
Expand Down Expand Up @@ -533,6 +530,8 @@ Render route with parameters into a path.
The L<Mojolicious::Routes> object this route is an ancestor of.
#r->root->caching(0);
=head2 C<route>
my $route = $r->route('/:c/:a', a => qr/\w+/);
Expand All @@ -541,7 +540,6 @@ Add a new nested child to this route.
=head2 C<to>
my $to = $r->to;
$r = $r->to(action => 'foo');
$r = $r->to({action => 'foo'});
$r = $r->to('controller#action');
Expand Down Expand Up @@ -584,6 +582,8 @@ variations.
Restrict HTTP methods this route is allowed to handle, defaults to no
restrictions.
$r->route('/foo')->via(qw/GET POST/)->to('foo#bar');
=head2 C<waypoint>
my $r = $r->waypoint('/:c/:a', a => qr/\w+/);
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Static.pm
Expand Up @@ -145,16 +145,15 @@ sub _get_data_file {
return if $rel =~ /\.\w+\.\w+$/;

# Index DATA files
unless ($self->{data_files}) {
$self->{data_files} = {};
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$self->{data_files}->{$_} = $class
for keys %{Mojo::Command->get_all_data($class)};
$index->{$_} = $class for keys %{Mojo::Command->get_all_data($class)};
}
}

# Find file
return Mojo::Command->new->get_data($rel, $self->{data_files}->{$rel});
return Mojo::Command->get_data($rel, $self->{index}->{$rel});
}

sub _get_file {
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/home.t
Expand Up @@ -52,11 +52,11 @@ is $home->rel_dir('foo/bar'), catdir(splitdir($FindBin::Bin), 'foo', 'bar'),
'right path';

# List files
is first(sub { $_ =~ /Base1\.pm$/ }, @{$home->list_files('lib')}),
is first(sub {/Base1\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base1.pm', 'right result';
is first(sub { $_ =~ /Base2\.pm$/ }, @{$home->list_files('lib')}),
is first(sub {/Base2\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base2.pm', 'right result';
is first(sub { $_ =~ /Base3\.pm$/ }, @{$home->list_files('lib')}),
is first(sub {/Base3\.pm$/}, @{$home->list_files('lib')}),
'BaseTest/Base3.pm', 'right result';

# Slurp files
Expand Down

0 comments on commit 125ae67

Please sign in to comment.