[Bps-public-commit] r15430 - in Path-Dispatcher/trunk: lib/Path t
sartak at bestpractical.com
sartak at bestpractical.com
Mon Aug 25 16:11:49 EDT 2008
Author: sartak
Date: Mon Aug 25 16:11:45 2008
New Revision: 15430
Modified:
Path-Dispatcher/trunk/ (props changed)
Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
Path-Dispatcher/trunk/t/004-stages.t
Path-Dispatcher/trunk/t/010-return.t
Log:
r70286 at onn: sartak | 2008-08-25 14:00:19 -0400
Refactor out the qualifying-stage-names code
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:11:45 2008
@@ -47,12 +47,28 @@
},
);
-sub stages {
+sub stage_names {
my $self = shift;
return ('first', @{ $self->_stages }, 'last');
}
+sub stages {
+ my $self = shift;
+ my @stages;
+
+ for my $stage ($self->stage_names) {
+ for my $substage ('before', 'on', 'after') {
+ my $qualified_stage = $substage eq 'on'
+ ? $stage
+ : "${substage}_$stage";
+ push @stages, $qualified_stage;
+ }
+ }
+
+ return @stages;
+}
+
sub dispatch {
my $self = shift;
my $path = shift;
@@ -66,32 +82,26 @@
for $self->rules;
for my $stage ($self->stages) {
- for my $substage ('before', 'on', 'after') {
- my $qualified_stage = $substage eq 'on'
- ? $stage
- : "${substage}_$stage";
-
- $self->begin_stage($qualified_stage, \@matches);
+ $self->begin_stage($stage, \@matches);
- for my $rule (@{ delete $rules_for_stage{$qualified_stage}||[] }) {
- my $vars = $rule->match($path)
- or next;
-
- $dispatch->add_match(
- stage => $qualified_stage,
- rule => $rule,
- result => $vars,
- );
- }
-
- if ($self->defer_to_super_dispatcher($qualified_stage, \@matches)) {
- $dispatch->add_redispatch(
- $self->super_dispatcher->dispatch($path)
- );
- }
+ for my $rule (@{ delete $rules_for_stage{$stage}||[] }) {
+ my $vars = $rule->match($path)
+ or next;
+
+ $dispatch->add_match(
+ stage => $stage,
+ rule => $rule,
+ result => $vars,
+ );
+ }
- $self->end_stage($qualified_stage, \@matches);
+ if ($self->defer_to_super_dispatcher($stage, \@matches)) {
+ $dispatch->add_redispatch(
+ $self->super_dispatcher->dispatch($path)
+ );
}
+
+ $self->end_stage($stage, \@matches);
}
warn "Unhandled stages: " . join(', ', keys %rules_for_stage)
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:11:45 2008
@@ -7,20 +7,16 @@
my @calls;
my $dispatcher = Path::Dispatcher->new;
-for my $stage (qw/first on last/) {
- for my $substage (qw/before on after/) {
- my $qualified_stage = $substage eq 'on'
- ? $stage
- : "${substage}_$stage";
-
- $dispatcher->add_rule(
- Path::Dispatcher::Rule::Regex->new(
- stage => $qualified_stage,
- regex => qr/foo/,
- block => sub { push @calls, $qualified_stage },
- ),
- );
- }
+for my $stage (qw/before_first first after_first
+ before_on on after_on
+ before_last last after_last/) {
+ $dispatcher->add_rule(
+ Path::Dispatcher::Rule::Regex->new(
+ stage => $stage,
+ regex => qr/foo/,
+ block => sub { push @calls, $stage },
+ ),
+ );
}
$dispatcher->run('foo');
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:11:45 2008
@@ -19,19 +19,16 @@
my $dispatch = $dispatcher->dispatch('foo');
is_deeply([$dispatch->run(24)], []);
-for my $stage (qw/first on last/) {
- for my $substage (qw/before on after/) {
- my $qualified_stage = $substage eq 'on'
- ? $stage
- : "${substage}_$stage";
- $dispatcher->add_rule(
- Path::Dispatcher::Rule::Regex->new(
- stage => $qualified_stage,
- regex => qr/foo/,
- block => sub { return @_ },
- ),
- );
- }
+for my $stage (qw/before_first first after_first
+ before_on on after_on
+ before_last last after_last/) {
+ $dispatcher->add_rule(
+ Path::Dispatcher::Rule::Regex->new(
+ stage => $stage,
+ regex => qr/foo/,
+ block => sub { return @_ },
+ ),
+ );
}
is_deeply([$dispatcher->run('foo', 42)], []);
More information about the Bps-public-commit
mailing list