[Catalyst-commits] r10659 - in Catalyst-Runtime/5.80/trunk: . lib/Catalyst lib/Catalyst/DispatchType t/aggregate t/lib t/lib/TestAppIndexDefault t/lib/TestAppIndexDefault/Controller

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Thu Jun 25 09:37:36 GMT 2009


Author: caelum
Date: 2009-06-25 09:37:35 +0000 (Thu, 25 Jun 2009)
New Revision: 10659

Added:
   Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_index_or_default.t
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Default.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexChained.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm
   Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Root.pm
Modified:
   Catalyst-Runtime/5.80/trunk/
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Action.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Default.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Index.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Path.pm
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm
Log:
 r5555 at hlagh (orig r10585):  caelum | 2009-06-18 12:42:15 -0700
 new branch
 r5556 at hlagh (orig r10586):  caelum | 2009-06-18 14:38:40 -0700
 made a failing test
 r5585 at hlagh (orig r10600):  caelum | 2009-06-19 16:47:02 -0700
 prevent actions named index from registering as an index dispatchtype if they can be registered with another dispatchtype
 r5586 at hlagh (orig r10601):  caelum | 2009-06-19 17:19:18 -0700
 minor change
 r5587 at hlagh (orig r10602):  caelum | 2009-06-19 22:11:53 -0700
 nicer action sorting for Path
 r5595 at hlagh (orig r10604):  caelum | 2009-06-20 10:18:53 -0700
 minor changes
 r5599 at hlagh (orig r10608):  caelum | 2009-06-20 17:26:22 -0700
 fix root default thingie jayk gave me, sanitize Paths at registration time better
 r5601 at hlagh (orig r10610):  caelum | 2009-06-20 18:06:01 -0700
 forgot to add a file
 r5616 at hlagh (orig r10611):  caelum | 2009-06-21 19:17:29 -0700
 missing / in test file
 r5676 at hlagh (orig r10645):  caelum | 2009-06-24 17:41:38 -0700
 add C::DispatchType::_is_low_precedence



Property changes on: Catalyst-Runtime/5.80/trunk
___________________________________________________________________
Name: svk:merge
   - 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/compres:7999
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/context_go:8001
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/trunk:8533
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/Catalyst-Test-Updates:8363
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/add_captures_to_visit:9546
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/moose:7911
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst:4483
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
8a9521aa-ff93-41d6-9f87-b05cafcdab40:/local/cat/Catalyst-Runtime/5.80/trunk:8157
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442
   + 1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst/trunk/Catalyst-Runtime:9763
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/compres:7999
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/branches/context_go:8001
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.70/trunk:8533
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/Catalyst-Test-Updates:8363
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/add_captures_to_visit:9546
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/index_default_fuckage:10646
4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Runtime/5.80/branches/moose:7911
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-ChildOf:4443
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-Runtime-jrockway:5857
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-component-setup:4320
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/Catalyst-docs:4325
4ad37cd2-5fec-0310-835f-b3785c72a374:/branches/current/Catalyst-Runtime:5142
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst:4483
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Runtime:6165
8a9521aa-ff93-41d6-9f87-b05cafcdab40:/local/cat/Catalyst-Runtime/5.80/trunk:8157
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime:8339
d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst/Catalyst-Runtime-jrockway:8342
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime:6511
e56d974f-7718-0410-8b1c-b347a71765b2:/local/Catalyst-Runtime-current:10442

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Action.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Action.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Action.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -18,8 +18,9 @@
 =cut
 
 use Moose;
-
+use Scalar::Util 'looks_like_number';
 with 'MooseX::Emulate::Class::Accessor::Fast';
+use namespace::clean -except => 'meta';
 
 has class => (is => 'rw');
 has namespace => (is => 'rw');
@@ -28,8 +29,6 @@
 has name => (is => 'rw');
 has code => (is => 'rw');
 
-no Moose;
-
 use overload (
 
     # Stringify to reverse for debug output etc.
@@ -38,6 +37,10 @@
     # Codulate to execute to invoke the encapsulated action coderef
     '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
 
+    # Which action takes precedence
+    'cmp' => 'compare',
+    '<=>' => 'compare',
+
     # Make general $stuff still work
     fallback => 1,
 
@@ -70,6 +73,22 @@
     return scalar( @{ $c->req->args } ) == $args;
 }
 
+sub sort_order {
+    my $self = shift;
+
+    my ($args) = @{ $self->attributes->{Args} || [] };
+
+    return $args if looks_like_number($args);
+
+    return ~0;
+}
+
+sub compare {
+    my ($a1, $a2) = @_;
+
+    return $a1->sort_order <=> $a2->sort_order;
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
@@ -105,6 +124,14 @@
 Check Args attribute, and makes sure number of args matches the setting.
 Always returns true if Args is omitted.
 
+=head2 sort_order
+
+Returns the value of the C<Args> attribute, or C<~0> if it has no value.
+
+=head2 compare
+
+Returns C<< $a->sort_order <=> $b->sort_order >> .
+
 =head2 namespace
 
 Returns the private namespace this action lives in.

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Default.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Default.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Default.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -41,7 +41,7 @@
 
 sub match {
     my ( $self, $c, $path ) = @_;
-    return if $path =~ m!/!;    # Not at root yet, wait for it ...
+    return if $path ne '';    # Not at root yet, wait for it ...
     my $result = ( $c->get_actions( 'default', $c->req->path ) )[-1];
 
     # Find default on namespace or super
@@ -58,6 +58,8 @@
     return 0;
 }
 
+sub _is_low_precedence { 1 }
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Index.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Index.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Index.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -2,7 +2,7 @@
 
 use Moose;
 extends 'Catalyst::DispatchType';
-no Moose;
+use namespace::clean -except => 'meta';
 
 =head1 NAME
 
@@ -25,6 +25,12 @@
 
 =back
 
+=cut
+
+has _actions => (
+    is => 'rw', isa => 'HashRef', default => sub { +{} }
+);
+
 =head1 METHODS
 
 =head2 $self->match( $c, $path )
@@ -40,6 +46,8 @@
     return if @{ $c->req->args };
     my $result = $c->get_action( 'index', $path );
 
+    return 0 unless $result && exists $self->_actions->{ $result->reverse };
+
     if ($result && $result->match($c)) {
         $c->action($result);
         $c->namespace( $result->namespace );
@@ -50,6 +58,20 @@
     return 0;
 }
 
+=head2 $self->register( $c, $action )
+
+Register an action with this DispatchType.
+
+=cut
+
+sub register {
+    my ( $self, $c, $action ) = @_;
+
+    $self->_actions->{ $action->reverse } = $action;
+
+    return 1;
+}
+
 =head2 $self->uri_for_action( $action, $captures )
 
 get a URI part for an action; always returns undef is $captures is set
@@ -67,6 +89,8 @@
     return "/".$action->namespace;
 }
 
+sub _is_low_precedence { 1 }
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Path.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Path.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType/Path.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -6,7 +6,6 @@
 use Text::SimpleTable;
 use Catalyst::Utils;
 use URI;
-use Scalar::Util ();
 
 has _paths => (
                is => 'rw',
@@ -62,16 +61,6 @@
       if ( keys %{ $self->_paths } );
 }
 
-sub _action_args_sort_order {
-    my ( $self, $action ) = @_;
-
-    my ($args) = @{ $action->attributes->{Args} || [] };
-
-    return $args if Scalar::Util::looks_like_number($args);
-
-    return ~0;
-}
-
 =head2 $self->match( $c, $path )
 
 For each action registered to this exact path, offers the action a chance to
@@ -85,10 +74,7 @@
 
     $path = '/' if !defined $path || !length $path;
 
-    # sort from least args to most
-    my @actions = sort { $self->_action_args_sort_order($a) <=>
-                         $self->_action_args_sort_order($b) }
-            @{ $self->_paths->{$path} || [] };
+    my @actions = @{ $self->_paths->{$path} || [] };
 
     foreach my $action ( @actions ) {
         next unless $action->match($c);
@@ -130,8 +116,11 @@
     $path =~ s!^/!!;
     $path = '/' unless length $path;
     $path = URI->new($path)->canonical;
+    $path =~ s{(?<=[^/])/+\z}{};
 
-    unshift( @{ $self->_paths->{$path} ||= [] }, $action);
+    $self->_paths->{$path} = [
+        sort { $a <=> $b } ($action, @{ $self->_paths->{$path} || [] })
+    ];
 
     return 1;
 }

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/DispatchType.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -73,6 +73,8 @@
 
 sub expand_action { }
 
+sub _is_low_precedence { 0 }
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm	2009-06-25 08:39:39 UTC (rev 10658)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Dispatcher.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -370,10 +370,8 @@
 
   DESCEND: while (@path) {
         $path = join '/', @path;
-        $path =~ s#^/##;
+        $path =~ s#^/+##;
 
-        $path = '' if $path eq '/';    # Root action
-
         # Check out dispatch types to see if any will handle the path at
         # this level
 
@@ -459,9 +457,6 @@
     }
 
     return reverse grep { defined } @containers, $self->_container_hash->{''};
-
-    #return (split '/', $namespace); # isnt this more clear?
-    my @parts = split '/', $namespace;
 }
 
 =head2 $self->uri_for_action($action, \@captures)
@@ -531,11 +526,30 @@
         }
     }
 
+    my @dtypes = @{ $self->_dispatch_types };
+    my @normal_dtypes;
+    my @low_precedence_dtypes;
+
+    for my $type ( @dtypes ) {
+        if ($type->_is_low_precedence) {
+            push @low_precedence_dtypes, $type;
+        } else {
+            push @normal_dtypes, $type;
+        }
+    }
+
     # Pass the action to our dispatch types so they can register it if reqd.
-    foreach my $type ( @{ $self->_dispatch_types } ) {
-        $type->register( $c, $action );
+    my $was_registered = 0;
+    foreach my $type ( @normal_dtypes ) {
+        $was_registered = 1 if $type->register( $c, $action );
     }
 
+    if (not $was_registered) {
+        foreach my $type ( @low_precedence_dtypes ) {
+            $type->register( $c, $action );
+        }
+    }
+
     my $namespace = $action->namespace;
     my $name      = $action->name;
 

Added: Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_index_or_default.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_index_or_default.t	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/aggregate/live_component_controller_action_index_or_default.t	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,40 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+
+our $iters;
+
+BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
+
+use Test::More tests => 6*$iters;
+
+use Catalyst::Test 'TestAppIndexDefault';
+
+if ( $ENV{CAT_BENCHMARK} ) {
+    require Benchmark;
+    Benchmark::timethis( $iters, \&run_tests );
+}
+else {
+    for ( 1 .. $iters ) {
+        run_tests();
+    }
+}
+
+sub run_tests {
+    is(get('/indexchained'), 'index_chained', ':Chained overrides index');
+    is(get('/indexprivate'), 'index_private', 'index : Private still works');
+
+# test :Path overriding default
+    is(get('/one_arg'), 'path_one_arg', ':Path overrides default');
+    is(get('/one_arg/foo/bar'), 'default', 'default still works');
+
+# now the same thing with a namespace, and a trailing / on the :Path
+    is(get('/default/one_arg'), 'default_path_one_arg',
+        ':Path overrides default');
+    is(get('/default/one_arg/foo/bar'), 'default_default',
+        'default still works');
+}

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Default.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Default.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Default.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,15 @@
+package TestAppIndexDefault::Controller::Default;
+
+use base 'Catalyst::Controller';
+
+sub default : Private {
+    my ($self, $c) = @_;
+    $c->res->body('default_default');
+}
+
+sub path_one_arg : Path('/default/') Args(1) {
+    my ($self, $c) = @_;
+    $c->res->body('default_path_one_arg');
+}
+
+1;

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexChained.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexChained.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexChained.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,12 @@
+package TestAppIndexDefault::Controller::IndexChained;
+
+use base 'Catalyst::Controller';
+
+sub index : Chained('/') PathPart('indexchained') CaptureArgs(0) {}
+
+sub index_endpoint : Chained('index') PathPart('') Args(0) {
+    my ($self, $c) = @_;
+    $c->res->body('index_chained');
+}
+
+1;

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,10 @@
+package TestAppIndexDefault::Controller::IndexPrivate;
+
+use base 'Catalyst::Controller';
+
+sub index : Private {
+    my ($self, $c) = @_;
+    $c->res->body('index_private');
+}
+
+1;

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Root.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Root.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault/Controller/Root.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,17 @@
+package TestAppIndexDefault::Controller::Root;
+
+use base 'Catalyst::Controller';
+
+__PACKAGE__->config->{namespace} = '';
+
+sub default : Private {
+    my ($self, $c) = @_;
+    $c->res->body('default');
+}
+
+sub path_one_arg : Path('/') Args(1) {
+    my ($self, $c) = @_;
+    $c->res->body('path_one_arg');
+}
+
+1;

Added: Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault.pm	                        (rev 0)
+++ Catalyst-Runtime/5.80/trunk/t/lib/TestAppIndexDefault.pm	2009-06-25 09:37:35 UTC (rev 10659)
@@ -0,0 +1,8 @@
+package TestAppIndexDefault;
+use strict;
+use warnings;
+use Catalyst;
+
+__PACKAGE__->setup;
+
+1;




More information about the Catalyst-commits mailing list