Router-Dumb
view release on metacpan or search on metacpan
lib/Router/Dumb/Route.pm view on Meta::CPAN
#pod =cut
has constraints => (
isa => 'HashRef',
default => sub { {} },
traits => [ 'Hash' ],
handles => {
constraint_names => 'keys',
constraint_for => 'get',
},
);
sub BUILD {
my ($self) = @_;
confess "multiple asterisk parts in route"
if (grep { $_ eq '*' } $self->parts) > 1;
my %seen;
$seen{$_}++ for grep { $_ =~ /^:/ } $self->parts;
my @repeated = grep { $seen{$_} > 1 } keys %seen;
confess "some path match names were repeated: @repeated" if @repeated;
my @bad_constraints;
for my $key ($self->constraint_names) {
push @bad_constraints, $key unless $seen{ ":$key" };
}
if (@bad_constraints) {
confess "constraints were given for unknown names: @bad_constraints";
}
}
sub _match {
my ($self, $matches) = @_;
$matches //= {};
return Router::Dumb::Match->new({
route => $self,
matches => $matches,
});
}
#pod =method check
#pod
#pod my $match_or_undef = $route->check( $str );
#pod
#pod This is the method used by the router to see if each route will accept the
#pod string. If it matches, it returns a L<match object|Router::Dumb::Match>.
#pod Otherwise, it returns false.
#pod
#pod =cut
sub check {
my ($self, $str) = @_;
return $self->_match if $str eq join(q{/}, $self->parts);
my %matches;
my @in_parts = split m{/}, $str;
my @my_parts = $self->parts;
PART: for my $i (keys @my_parts) {
my $my_part = $my_parts[ $i ];
if ($my_part ne '*' and $my_part !~ /^:/) {
return unless $my_part eq $in_parts[$i];
next PART;
}
if ($my_parts[$i] eq '*') {
$matches{REST} = join q{/}, @in_parts[ $i .. $#in_parts ];
return $self->_match(\%matches);
}
confess 'unreachable condition' unless $my_parts[$i] =~ /^:(.+)/;
my $name = $1;
my $value = $in_parts[ $i ];
if (my $constraint = $self->constraint_for($name)) {
return unless $constraint->check($value);
}
$matches{ $name } = $value;
}
return $self->_match(\%matches);
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Router::Dumb::Route - just one dumb route for use in a big dumb router
=head1 VERSION
version 0.006
=head1 OVERVIEW
Router::Dumb::Route objects represent paths that a L<Router::Dumb> object can
route to. They are usually created by calling the
C<L<add_route|Router::Dumb/add_route>> method on a router.
=head1 PERL VERSION
This library should run on perls released even a long time ago. It should work
on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased. The version may be increased
for any reason, and there is no promise that patches will be accepted to lower
the minimum required perl.
( run in 1.235 second using v1.01-cache-2.11-cpan-5511b514fd6 )