nicer action sorting for Path
Rafael Kitover [Sat, 20 Jun 2009 05:11:53 +0000 (05:11 +0000)]
lib/Catalyst/Action.pm
lib/Catalyst/DispatchType/Path.pm
t/aggregate/live_component_controller_action_index_or_default.t [moved from t/aggregate/live_component_controller_action_named_index.t with 61% similarity]
t/lib/TestAppIndexDefault.pm [moved from t/lib/TestAppIndexActionName.pm with 67% similarity]
t/lib/TestAppIndexDefault/Controller/DefaultAndPath.pm [new file with mode: 0644]
t/lib/TestAppIndexDefault/Controller/IndexChained.pm [moved from t/lib/TestAppIndexActionName/Controller/IndexChained.pm with 80% similarity]
t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm [new file with mode: 0644]

index e2e78b6..0eb9942 100644 (file)
@@ -18,8 +18,9 @@ L<Catalyst::Controller> subclasses.
 =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 attributes => (is => 'rw');
 has name => (is => 'rw');
 has code => (is => 'rw');
 
-no Moose;
-
 use overload (
 
     # Stringify to reverse for debug output etc.
@@ -38,6 +37,10 @@ use overload (
     # 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 @@ sub match {
     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;
index 6be3893..50fbf84 100644 (file)
@@ -6,7 +6,6 @@ extends 'Catalyst::DispatchType';
 use Text::SimpleTable;
 use Catalyst::Utils;
 use URI;
-use Scalar::Util ();
 
 has _paths => (
                is => 'rw',
@@ -62,16 +61,6 @@ sub list {
       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,12 +74,7 @@ sub match {
 
     $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} || [] };
-
-    foreach my $action ( @actions ) {
+    foreach my $action ( @{ $self->_paths->{$path} || [] } ) {
         next unless $action->match($c);
         $c->req->action($path);
         $c->req->match($path);
@@ -133,6 +117,8 @@ sub register_path {
 
     unshift( @{ $self->_paths->{$path} ||= [] }, $action);
 
+    $self->_paths->{$path} = [ sort @{ $self->_paths->{$path} } ];
+
     return 1;
 }
 
@@ -10,9 +10,9 @@ our $iters;
 
 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
 
-use Test::More tests => 1*$iters;
+use Test::More tests => 3*$iters;
 
-use Catalyst::Test 'TestAppIndexActionName';
+use Catalyst::Test 'TestAppIndexDefault';
 
 if ( $ENV{CAT_BENCHMARK} ) {
     require Benchmark;
@@ -26,4 +26,7 @@ else {
 
 sub run_tests {
     is(get('/indexchained'), 'index_chained', ':Chained overrides index');
+    is(get('/indexprivate'), 'index_private', 'index : Private still works');
+    is(get('/defaultandpath/path_one_arg'), 'path_one_arg',
+        'Path overrides default');
 }
similarity index 67%
rename from t/lib/TestAppIndexActionName.pm
rename to t/lib/TestAppIndexDefault.pm
index 977693e..9a129cb 100644 (file)
@@ -1,4 +1,4 @@
-package TestAppIndexActionName;
+package TestAppIndexDefault;
 use strict;
 use warnings;
 use Catalyst;
diff --git a/t/lib/TestAppIndexDefault/Controller/DefaultAndPath.pm b/t/lib/TestAppIndexDefault/Controller/DefaultAndPath.pm
new file mode 100644 (file)
index 0000000..385361c
--- /dev/null
@@ -0,0 +1,15 @@
+package TestAppIndexDefault::Controller::DefaultAndPath;
+
+use base 'Catalyst::Controller';
+
+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;
@@ -1,4 +1,4 @@
-package TestAppIndexActionName::Controller::IndexChained;
+package TestAppIndexDefault::Controller::IndexChained;
 
 use base 'Catalyst::Controller';
 
diff --git a/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm b/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm
new file mode 100644 (file)
index 0000000..08367ff
--- /dev/null
@@ -0,0 +1,10 @@
+package TestAppIndexDefault::Controller::IndexPrivate;
+
+use base 'Catalyst::Controller';
+
+sub index : Private {
+    my ($self, $c) = @_;
+    $c->res->body('index_private');
+}
+
+1;