nicer action sorting for Path
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
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;