Initial sketch from work code
Tomas Doran [Mon, 26 Dec 2011 23:13:14 +0000 (23:13 +0000)]
Makefile.PL
lib/CatalystX/UriFor/Curried.pm

index 37a96e9..623603b 100644 (file)
@@ -8,6 +8,8 @@ name 'CatalystX-UriFor-Curried';
 all_from 'lib/CatalystX/UriFor/Curried.pm';
 
 requires 'Moose';
+requires 'MooseX::Types';
+requires 'MooseX::Autobox';
 requires 'namespace::autoclean';
 
 build_requires 'Catalyst::Runtime' => '5.80015';
index 3de3487..e8e5db5 100644 (file)
@@ -1,7 +1,41 @@
 package CatalystX::UriFor::Curried;
-use Moose;
+use MooseX::Role::Parameterized;
+use Moose::Autobox;
+use MooseX::Types::Moose qw/ Str ArrayRef /;
+use Scalar::Util qw/ weaken /;
 use namespace::autoclean;
 
+parameter method_name => (
+    isa => Str,
+    required => 1,
+);
+
+parameter stash_captures => (
+    isa => ArrayRef[Str],
+    required => 1,
+);
+
+role {
+    my $p = shift;
+
+    my $method_name = $p->method_name;
+    my $stash_captures => $p->stash_captures;
+
+    method $method_name => sub {
+        my ($ctx, $action_or_private_path, $captures, @args)  = @_;
+        my $weak_ctx = $ctx; # Just for safety so it can't be closed over.
+        weaken($weak_ctx);
+        if (ref($captures) eq 'ARRAY' || (!$captures && !scalar(@args))) { # FIXME BUG IN THIS LOGIC
+            $captures ||= [];
+            foreach my $name_or_sub ($stash_captures->flatten) {
+                unshift(@$captures, ref($name_or_sub) eq 'CODE' ? $name_or_sub->($weak_ctx) : $ctx->stash->{$name_or_sub});
+            }
+            $ctx->uri_for_action($action_or_private_path, $captures, @args);
+        }
+    };
+
+};
+
 1;
 
 =head1 NAME