X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalystX%2FUriFor%2FCurried.pm;h=e8e5db5bf40cfbb8a5b92cf0dcfdd3e0ce0a797b;hb=c575a0d740aa74794f62c00158124258292b16e9;hp=3de3487144a9e8dd4be16073519aa2793e6e7b0f;hpb=a42fd357039a10977791f835eaa47c40e6dcea6b;p=catagits%2FCatalystX-UriFor-Curried.git diff --git a/lib/CatalystX/UriFor/Curried.pm b/lib/CatalystX/UriFor/Curried.pm index 3de3487..e8e5db5 100644 --- a/lib/CatalystX/UriFor/Curried.pm +++ b/lib/CatalystX/UriFor/Curried.pm @@ -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