[Bps-public-commit] r15437 - in Path-Dispatcher/trunk: lib/Path lib/Path/Dispatcher t
sartak at bestpractical.com
sartak at bestpractical.com
Mon Aug 25 16:13:11 EDT 2008
Author: sartak
Date: Mon Aug 25 16:13:07 2008
New Revision: 15437
Modified:
Path-Dispatcher/trunk/ (props changed)
Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm
Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm
Path-Dispatcher/trunk/lib/Path/Dispatcher/Stage.pm
Path-Dispatcher/trunk/t/001-api.t
Path-Dispatcher/trunk/t/003-404.t
Path-Dispatcher/trunk/t/004-stages.t
Path-Dispatcher/trunk/t/005-multi-rule.t
Path-Dispatcher/trunk/t/006-abort.t
Path-Dispatcher/trunk/t/007-coderef-matcher.t
Path-Dispatcher/trunk/t/008-super-dispatcher.t
Path-Dispatcher/trunk/t/009-args.t
Path-Dispatcher/trunk/t/010-return.t
Path-Dispatcher/trunk/t/101-subclass.t
Log:
r70293 at onn: sartak | 2008-08-25 14:50:48 -0400
Rules now belong to stages. Rules are now also stage-agnostic. Much cleaner code and concepts :)
Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher.pm (original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher.pm Mon Aug 25 16:13:07 2008
@@ -1,7 +1,6 @@
#!/usr/bin/env perl
package Path::Dispatcher;
use Moose;
-use MooseX::AttributeHelpers;
use Path::Dispatcher::Stage;
use Path::Dispatcher::Rule;
@@ -10,17 +9,6 @@
sub stage_class { 'Path::Dispatcher::Stage' }
sub dispatch_class { 'Path::Dispatcher::Dispatch' }
-has _rules => (
- metaclass => 'Collection::Array',
- is => 'rw',
- isa => 'ArrayRef[Path::Dispatcher::Rule]',
- default => sub { [] },
- provides => {
- push => 'add_rule',
- elements => 'rules',
- },
-);
-
has super_dispatcher => (
is => 'rw',
isa => 'Path::Dispatcher',
@@ -39,7 +27,6 @@
);
has stages => (
- metaclass => 'Collection::Array',
is => 'rw',
isa => 'ArrayRef[Path::Dispatcher::Stage]',
auto_deref => 1,
@@ -65,16 +52,24 @@
return \@stages;
}
+# ugh, we should probably use IxHash..
+sub stage {
+ my $self = shift;
+ my $name = shift;
+
+ for my $stage ($self->stages) {
+ return $stage if $stage->qualified_name eq $name;
+ }
+
+ return;
+}
+
sub dispatch {
my $self = shift;
my $path = shift;
my $dispatch = $self->dispatch_class->new;
- my %rules_for_stage;
- push @{ $rules_for_stage{$_->stage_name} }, $_
- for $self->rules;
-
STAGE:
for my $stage ($self->stages) {
$self->begin_stage(
@@ -85,7 +80,7 @@
my $stage_name = $stage->qualified_name;
RULE:
- for my $rule (@{ delete $rules_for_stage{$stage_name} || [] }) {
+ for my $rule ($stage->rules) {
my $vars = $rule->match($path)
or next;
@@ -110,9 +105,6 @@
);
}
- warn "Unhandled stages: " . join(', ', keys %rules_for_stage)
- if keys %rules_for_stage;
-
return $dispatch;
}
Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm (original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm Mon Aug 25 16:13:07 2008
@@ -58,7 +58,7 @@
$dispatcher->run(@_);
},
on => sub {
- $dispatcher->add_rule(
+ $dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => $_[0],
block => $_[1],
@@ -66,18 +66,16 @@
);
},
before => sub {
- $dispatcher->add_rule(
+ $dispatcher->stage('first')->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => 'first',
regex => $_[0],
block => $_[1],
),
);
},
after => sub {
- $dispatcher->add_rule(
+ $dispatcher->stage('last')->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => 'last',
regex => $_[0],
block => $_[1],
),
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 Mon Aug 25 16:13:07 2008
@@ -4,35 +4,12 @@
use Path::Dispatcher::Stage;
-has stage => (
- is => 'ro',
- isa => 'Str|Path::Dispatcher::Stage',
- default => 'on',
- required => 1,
-);
-
has block => (
is => 'ro',
isa => 'CodeRef',
required => 1,
);
-has fallthrough => (
- is => 'ro',
- isa => 'Bool',
- lazy => 1,
- default => sub {
- my $self = shift;
- $self->stage eq 'on' ? 0 : 1;
- },
-);
-
-sub stage_name {
- my $stage = shift->stage;
- return $stage if !ref($stage);
- return $stage->qualified_name;
-}
-
sub match {
my $self = shift;
my $path = shift;
Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Stage.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Stage.pm (original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Stage.pm Mon Aug 25 16:13:07 2008
@@ -2,6 +2,8 @@
package Path::Dispatcher::Stage;
use Moose;
+use Path::Dispatcher::Rule;
+
has name => (
is => 'ro',
isa => 'Str',
@@ -13,6 +15,17 @@
predicate => 'is_qualified',
);
+has _rules => (
+ metaclass => 'Collection::Array',
+ is => 'rw',
+ isa => 'ArrayRef[Path::Dispatcher::Rule]',
+ default => sub { [] },
+ provides => {
+ push => 'add_rule',
+ elements => 'rules',
+ },
+);
+
sub qualified_name {
my $self = shift;
my $name = $self->name;
Modified: Path-Dispatcher/trunk/t/001-api.t
==============================================================================
--- Path-Dispatcher/trunk/t/001-api.t (original)
+++ Path-Dispatcher/trunk/t/001-api.t Mon Aug 25 16:13:07 2008
@@ -7,7 +7,7 @@
my @calls;
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub { push @calls, [@_] },
@@ -26,7 +26,7 @@
$dispatcher->run('foo');
is_deeply([splice @calls], [ [] ], "invoked the rule block on 'run'");
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/(bar)/,
block => sub { push @calls, [$1, $2] },
Modified: Path-Dispatcher/trunk/t/003-404.t
==============================================================================
--- Path-Dispatcher/trunk/t/003-404.t (original)
+++ Path-Dispatcher/trunk/t/003-404.t Mon Aug 25 16:13:07 2008
@@ -7,7 +7,7 @@
my @calls;
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub { push @calls, [@_] },
Modified: Path-Dispatcher/trunk/t/004-stages.t
==============================================================================
--- Path-Dispatcher/trunk/t/004-stages.t (original)
+++ Path-Dispatcher/trunk/t/004-stages.t Mon Aug 25 16:13:07 2008
@@ -10,9 +10,8 @@
for my $stage (qw/before_first first after_first
before_on on after_on
before_last last after_last/) {
- $dispatcher->add_rule(
+ $dispatcher->stage($stage)->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => $stage,
regex => qr/foo/,
block => sub { push @calls, $stage },
),
Modified: Path-Dispatcher/trunk/t/005-multi-rule.t
==============================================================================
--- Path-Dispatcher/trunk/t/005-multi-rule.t (original)
+++ Path-Dispatcher/trunk/t/005-multi-rule.t Mon Aug 25 16:13:07 2008
@@ -9,9 +9,8 @@
my $dispatcher = Path::Dispatcher->new;
for my $stage (qw/first on last/) {
for my $number (qw/first second/) {
- $dispatcher->add_rule(
+ $dispatcher->stage($stage)->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => $stage,
regex => qr/foo/,
block => sub { push @calls, "$stage: $number" },
),
Modified: Path-Dispatcher/trunk/t/006-abort.t
==============================================================================
--- Path-Dispatcher/trunk/t/006-abort.t (original)
+++ Path-Dispatcher/trunk/t/006-abort.t Mon Aug 25 16:13:07 2008
@@ -8,7 +8,7 @@
my @calls;
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub {
@@ -18,9 +18,8 @@
),
);
-$dispatcher->add_rule(
+$dispatcher->stage('last')->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => 'last',
regex => qr/foo/,
block => sub {
push @calls, "last";
@@ -39,7 +38,7 @@
};
is_deeply([splice @calls], ['on'], "correctly aborted the entire dispatch");
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/bar/,
block => sub {
Modified: Path-Dispatcher/trunk/t/007-coderef-matcher.t
==============================================================================
--- Path-Dispatcher/trunk/t/007-coderef-matcher.t (original)
+++ Path-Dispatcher/trunk/t/007-coderef-matcher.t Mon Aug 25 16:13:07 2008
@@ -7,7 +7,7 @@
my (@matches, @calls);
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::CodeRef->new(
matcher => sub { push @matches, $_; length > 5 },
block => sub { push @calls, [@_] },
Modified: Path-Dispatcher/trunk/t/008-super-dispatcher.t
==============================================================================
--- Path-Dispatcher/trunk/t/008-super-dispatcher.t (original)
+++ Path-Dispatcher/trunk/t/008-super-dispatcher.t Mon Aug 25 16:13:07 2008
@@ -18,9 +18,8 @@
is($sub_dispatcher->super_dispatcher, $super_dispatcher, "the super dispatcher is correct");
for my $stage (qw/before_on on after_on/) {
- $super_dispatcher->add_rule(
+ $super_dispatcher->stage($stage)->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => $stage,
regex => qr/foo/,
block => sub { push @calls, "super $stage" },
),
@@ -28,9 +27,8 @@
}
for my $stage (qw/before_on after_on/) {
- $sub_dispatcher->add_rule(
+ $sub_dispatcher->stage($stage)->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => $stage,
regex => qr/foo/,
block => sub { push @calls, "sub $stage" },
),
@@ -53,9 +51,8 @@
'sub after_on',
]);
-$sub_dispatcher->add_rule(
+$sub_dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => 'on',
regex => qr/foo/,
block => sub { push @calls, "sub on" },
),
Modified: Path-Dispatcher/trunk/t/009-args.t
==============================================================================
--- Path-Dispatcher/trunk/t/009-args.t (original)
+++ Path-Dispatcher/trunk/t/009-args.t Mon Aug 25 16:13:07 2008
@@ -7,7 +7,7 @@
my @calls;
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub { push @calls, [@_] },
Modified: Path-Dispatcher/trunk/t/010-return.t
==============================================================================
--- Path-Dispatcher/trunk/t/010-return.t (original)
+++ Path-Dispatcher/trunk/t/010-return.t Mon Aug 25 16:13:07 2008
@@ -7,7 +7,7 @@
# we currently have no defined return strategy :/
my $dispatcher = Path::Dispatcher->new;
-$dispatcher->add_rule(
+$dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub { return @_ },
@@ -22,9 +22,8 @@
for my $stage (qw/before_first first after_first
before_on on after_on
before_last last after_last/) {
- $dispatcher->add_rule(
+ $dispatcher->stage($stage)->add_rule(
Path::Dispatcher::Rule::Regex->new(
- stage => $stage,
regex => qr/foo/,
block => sub { return @_ },
),
Modified: Path-Dispatcher/trunk/t/101-subclass.t
==============================================================================
--- Path-Dispatcher/trunk/t/101-subclass.t (original)
+++ Path-Dispatcher/trunk/t/101-subclass.t Mon Aug 25 16:13:07 2008
@@ -23,7 +23,7 @@
'app after foo',
]);
-Path::Dispatcher::Test::App->dispatcher->add_rule(
+Path::Dispatcher::Test::App->dispatcher->stage('on')->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub {
More information about the Bps-public-commit
mailing list