Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
unparsed is a better attribute name
  • Loading branch information
kraih committed Feb 24, 2015
1 parent 366e645 commit 618c7b4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -23,6 +23,8 @@
- Removed deprecated render_exception and render_not_found methods from
Mojolicious::Controller.
- Removed deprecated keep_alive_requests setting from Hypnotoad.
- Renamed pattern attribute in Mojolicious::Routes::Route to unparsed.
- Renamed template attribute in Mojo::Template to unparsed.
- Renamed params method in Mojo::Parameters to pairs.
- Added names method to Mojo::Parameters.
- Added failed and passed methods to Mojolicious::Validator::Validation.
Expand Down
24 changes: 12 additions & 12 deletions lib/Mojo/Template.pm
Expand Up @@ -8,7 +8,7 @@ use Mojo::Util qw(decode encode monkey_patch slurp);

use constant DEBUG => $ENV{MOJO_TEMPLATE_DEBUG} || 0;

has [qw(append code prepend template)] => '';
has [qw(append code prepend unparsed)] => '';
has [qw(auto_escape compiled)];
has capture_end => 'end';
has capture_start => 'begin';
Expand Down Expand Up @@ -92,7 +92,7 @@ sub compile {
$self->compiled($compiled) and return undef unless $@;

# Use local stacktrace for compile exceptions
return Mojo::Exception->new($@, [$self->template, $code])->trace->verbose(1);
return Mojo::Exception->new($@, [$self->unparsed, $code])->trace->verbose(1);
}

sub interpret {
Expand All @@ -101,22 +101,22 @@ sub interpret {
# Stacktrace
local $SIG{__DIE__} = sub {
CORE::die($_[0]) if ref $_[0];
Mojo::Exception->throw(shift, [$self->template, $self->code]);
Mojo::Exception->throw(shift, [$self->unparsed, $self->code]);
};

return undef unless my $compiled = $self->compiled;
my $output;
return $output if eval { $output = $compiled->(@_); 1 };

# Exception with template context
return Mojo::Exception->new($@, [$self->template])->verbose(1);
return Mojo::Exception->new($@, [$self->unparsed])->verbose(1);
}

sub parse {
my ($self, $template) = @_;

# Clean start
$self->template($template)->tree(\my @tree);
$self->unparsed($template)->tree(\my @tree);

my $tag = $self->tag_start;
my $replace = $self->replace_mark;
Expand Down Expand Up @@ -580,13 +580,6 @@ Characters indicating the end of a tag, defaults to C<%E<gt>>.
<%= $foo %>
=head2 template
my $template = $mt->template;
$mt = $mt->template('<%= 1 + 1 %>');
Raw unparsed template.
=head2 tree
my $tree = $mt->tree;
Expand All @@ -604,6 +597,13 @@ Character activating automatic whitespace trimming, defaults to C<=>.
<%= $foo =%>
=head2 unparsed
my $unparsed = $mt->unparsed;
$mt = $mt->unparsed('<%= 1 + 1 %>');
Raw unparsed template.
=head1 METHODS
L<Mojo::Template> inherits all methods from L<Mojo::Base> and implements the
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/routes.pm
Expand Up @@ -24,7 +24,7 @@ sub _walk {
# Pattern
my $prefix = '';
if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' }
push @$rows, my $row = [$prefix . ($route->pattern->pattern || '/')];
push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')];

# Flags
my @flags;
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Routes::Pattern;
use Mojo::Base -base;

has [qw(constraints defaults)] => sub { {} };
has [qw(format_regex pattern regex)];
has [qw(format_regex regex unparsed)];
has placeholder_start => ':';
has [qw(placeholders tree)] => sub { [] };
has quote_end => ')';
Expand Down Expand Up @@ -215,7 +215,7 @@ sub _tokenize {
else { push @tree, ['text', $char] }
}

return $self->pattern($pattern)->tree(\@tree);
return $self->unparsed($pattern)->tree(\@tree);
}

1;
Expand Down Expand Up @@ -266,13 +266,6 @@ Default parameters.
Compiled regular expression for format matching.
=head2 pattern
my $raw = $pattern->pattern;
$pattern = $pattern->pattern('/(foo)/(bar)');
Raw unparsed pattern.
=head2 placeholder_start
my $start = $pattern->placeholder_start;
Expand Down Expand Up @@ -323,6 +316,13 @@ Character indicating a relaxed placeholder, defaults to C<#>.
Pattern in parsed form. Note that this structure should only be used very
carefully since it is very dynamic.
=head2 unparsed
my $unparsed = $pattern->unparsed;
$pattern = $pattern->unparsed('/(foo)/(bar)');
Raw unparsed pattern.
=head2 wildcard_start
my $start = $pattern->wildcard_start;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -94,7 +94,7 @@ sub over {

sub parse {
my $self = shift;
$self->{name} = $self->pattern->parse(@_)->pattern // '';
$self->{name} = $self->pattern->parse(@_)->unparsed // '';
$self->{name} =~ s/\W+//g;
return $self;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ sub to {
}

sub to_string {
join '', map { $_->pattern->pattern // '' } @{shift->_chain};
join '', map { $_->pattern->unparsed // '' } @{shift->_chain};
}

sub under { shift->_generate_route(under => @_) }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/templates/development.html.ep
Expand Up @@ -240,7 +240,7 @@
% my ($walk, $route, $depth) = @_;
<tr>
<td class="striped value">
% my $pattern = $route->pattern->pattern || '/';
% my $pattern = $route->pattern->unparsed || '/';
% $pattern = "+$pattern" if $depth;
<pre><%= ' ' x $depth %><%= $pattern %></pre>
</td>
Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/pattern.t
Expand Up @@ -62,7 +62,7 @@ is $pattern->render({a => 'c', b => 'd'}), '/test/c/123/d/456', 'right result';

# Root
$pattern = Mojolicious::Routes::Pattern->new('/');
is $pattern->pattern, undef, 'slash has been optimized away';
is $pattern->unparsed, undef, 'slash has been optimized away';
$pattern->defaults({action => 'index'});
ok !$pattern->match('/test/foo/bar'), 'no result';
is_deeply $pattern->match('/'), {action => 'index'}, 'right structure';
Expand Down

0 comments on commit 618c7b4

Please sign in to comment.