Skip to content

Commit

Permalink
support constraints as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Feb 21, 2014
1 parent 1887fc2 commit 0da9c83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/Routes/Tiny/Pattern.pm
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ sub _prepare_pattern {

if (exists $self->{constraints}->{$name}) {
$constraint = $self->{constraints}->{$name};
if (ref $constraint eq 'ARRAY') {
$constraint = '?:' . join('|', @$constraint);
}
$re .= "($constraint)";
}
else {
Expand Down
17 changes: 16 additions & 1 deletion t/match-with-constraints.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 5;
use Test::More;

use Routes::Tiny;

Expand All @@ -25,3 +25,18 @@ ok($@ =~ qr/Required param 'id' was not passed when building a path/);

eval { $r->build_path('article', id => 'abc'); };
ok($@ =~ qr/Param 'id' fails a constraint/);

subtest 'contraint as array' => sub {
my $r = Routes::Tiny->new;

$r->add_route(
'/articles/:id',
name => 'article',
constraints => {id => [qw/1 2 3/]}
);

ok $r->match('/articles/1');
ok!$r->match('/articles/a');
};

done_testing;

0 comments on commit 0da9c83

Please sign in to comment.