Initial sketch from work code
[catagits/CatalystX-UriFor-Curried.git] / lib / CatalystX / UriFor / Curried.pm
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