From: Rafael Kitover Date: Sat, 20 Jun 2009 05:11:53 +0000 (+0000) Subject: nicer action sorting for Path X-Git-Tag: 5.80006~44 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=05b47f2e6357bc7f1622e6b2cf730ad5bbeb993f;hp=7b442de55a57592a0a9f09db2e85b135bb0c123f nicer action sorting for Path --- diff --git a/lib/Catalyst/Action.pm b/lib/Catalyst/Action.pm index e2e78b6..0eb9942 100644 --- a/lib/Catalyst/Action.pm +++ b/lib/Catalyst/Action.pm @@ -18,8 +18,9 @@ L 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; diff --git a/lib/Catalyst/DispatchType/Path.pm b/lib/Catalyst/DispatchType/Path.pm index 6be3893..50fbf84 100644 --- a/lib/Catalyst/DispatchType/Path.pm +++ b/lib/Catalyst/DispatchType/Path.pm @@ -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; } diff --git a/t/aggregate/live_component_controller_action_named_index.t b/t/aggregate/live_component_controller_action_index_or_default.t similarity index 61% rename from t/aggregate/live_component_controller_action_named_index.t rename to t/aggregate/live_component_controller_action_index_or_default.t index 393e030..d385add 100644 --- a/t/aggregate/live_component_controller_action_named_index.t +++ b/t/aggregate/live_component_controller_action_index_or_default.t @@ -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'); } diff --git a/t/lib/TestAppIndexActionName.pm b/t/lib/TestAppIndexDefault.pm similarity index 67% rename from t/lib/TestAppIndexActionName.pm rename to t/lib/TestAppIndexDefault.pm index 977693e..9a129cb 100644 --- a/t/lib/TestAppIndexActionName.pm +++ b/t/lib/TestAppIndexDefault.pm @@ -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 index 0000000..385361c --- /dev/null +++ b/t/lib/TestAppIndexDefault/Controller/DefaultAndPath.pm @@ -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; diff --git a/t/lib/TestAppIndexActionName/Controller/IndexChained.pm b/t/lib/TestAppIndexDefault/Controller/IndexChained.pm similarity index 80% rename from t/lib/TestAppIndexActionName/Controller/IndexChained.pm rename to t/lib/TestAppIndexDefault/Controller/IndexChained.pm index 5dfb013..18a8034 100644 --- a/t/lib/TestAppIndexActionName/Controller/IndexChained.pm +++ b/t/lib/TestAppIndexDefault/Controller/IndexChained.pm @@ -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 index 0000000..08367ff --- /dev/null +++ b/t/lib/TestAppIndexDefault/Controller/IndexPrivate.pm @@ -0,0 +1,10 @@ +package TestAppIndexDefault::Controller::IndexPrivate; + +use base 'Catalyst::Controller'; + +sub index : Private { + my ($self, $c) = @_; + $c->res->body('index_private'); +} + +1;