[Bps-public-commit] r15580 - in Path-Dispatcher/trunk: lib/Path/Dispatcher lib/Path/Dispatcher/Rule t

sartak at bestpractical.com sartak at bestpractical.com
Wed Aug 27 22:50:24 EDT 2008


Author: sartak
Date: Wed Aug 27 22:50:23 2008
New Revision: 15580

Added:
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Intersection.pm
   Path-Dispatcher/trunk/t/012-intersection.t
Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm

Log:
 r70615 at onn:  sartak | 2008-08-27 22:50:19 -0400
 Add an "intersection" compound rule


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm	Wed Aug 27 22:50:23 2008
@@ -44,6 +44,7 @@
 require Path::Dispatcher::Rule::CodeRef;
 require Path::Dispatcher::Rule::Regex;
 require Path::Dispatcher::Rule::Tokens;
+require Path::Dispatcher::Rule::Intersection;
 
 1;
 

Added: Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Intersection.pm
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Intersection.pm	Wed Aug 27 22:50:23 2008
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+package Path::Dispatcher::Rule::Intersection;
+use Moose;
+use MooseX::AttributeHelpers;
+
+extends 'Path::Dispatcher::Rule';
+
+has _rules => (
+    metaclass => 'Collection::Array',
+    is        => 'rw',
+    isa       => 'ArrayRef[Path::Dispatcher::Rule]',
+    init_arg  => 'rules',
+    default   => sub { [] },
+    provides  => {
+        push     => 'add_rule',
+        elements => 'rules',
+    },
+);
+
+has '+block' => (
+    required => 0,
+);
+
+sub _match {
+    my $self = shift;
+    my $path = shift;
+
+    for my $rule ($self->rules) {
+        return 0 unless $rule->match($path);
+    }
+
+    return 1;
+}
+
+sub run {
+    my $self = shift;
+    my @rules = $self->rules;
+    for my $rule (@rules) {
+        $rule->run(@_);
+    }
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+

Added: Path-Dispatcher/trunk/t/012-intersection.t
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/t/012-intersection.t	Wed Aug 27 22:50:23 2008
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 12;
+use Path::Dispatcher;
+
+my @calls;
+
+my $rule = Path::Dispatcher::Rule::Intersection->new;
+my $dispatcher = Path::Dispatcher->new;
+$dispatcher->stage('on')->add_rule($rule);
+
+my $dispatch = $dispatcher->dispatch('foobar');
+my @matches = $dispatch->matches;
+is(@matches, 1, "got a match");
+is($matches[0]->rule, $rule, "empty intersection rule matches");
+$dispatch->run;
+is_deeply([splice @calls], [], "no calls yet..");
+
+$rule->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/foobar/,
+        block => sub { push @calls, 'foobar' },
+    ),
+);
+
+$dispatch = $dispatcher->dispatch('foobar');
+ at matches = $dispatch->matches;
+is(@matches, 1, "got a match");
+is($matches[0]->rule, $rule, "intersection rule matches");
+$dispatch->run;
+is_deeply([splice @calls], ['foobar'], "foobar block called");
+
+$dispatch = $dispatcher->dispatch('baz');
+ at matches = $dispatch->matches;
+is(@matches, 0, "no matches");
+
+$rule->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/baz/,
+        block => sub { push @calls, 'baz' },
+    ),
+);
+
+$dispatch = $dispatcher->dispatch('foobar');
+ at matches = $dispatch->matches;
+is(@matches, 0, "no matches, because we need to match foobar AND baz");
+
+$dispatch = $dispatcher->dispatch('baz');
+ at matches = $dispatch->matches;
+is(@matches, 0, "no matches, because we need to match foobar AND baz");
+
+$dispatch = $dispatcher->dispatch('foobarbaz');
+ at matches = $dispatch->matches;
+is(@matches, 1, "got a match");
+is($matches[0]->rule, $rule, "intersection rule matches");
+$dispatch->run;
+is_deeply([splice @calls], ['foobar', 'baz'], "both blocks called");
+



More information about the Bps-public-commit mailing list