Deal with param passing
[catagits/CatalystX-UriFor-Curried.git] / lib / CatalystX / UriFor / Curried.pm
CommitLineData
a42fd357 1package CatalystX::UriFor::Curried;
c575a0d7 2use MooseX::Role::Parameterized;
3use Moose::Autobox;
4use MooseX::Types::Moose qw/ Str ArrayRef /;
5use Scalar::Util qw/ weaken /;
a42fd357 6use namespace::autoclean;
7
c575a0d7 8parameter method_name => (
9 isa => Str,
10 required => 1,
11);
12
13parameter stash_captures => (
14 isa => ArrayRef[Str],
15 required => 1,
16);
17
18role {
19 my $p = shift;
20
21 my $method_name = $p->method_name;
22 my $stash_captures => $p->stash_captures;
23
24 method $method_name => sub {
25 my ($ctx, $action_or_private_path, $captures, @args) = @_;
26 my $weak_ctx = $ctx; # Just for safety so it can't be closed over.
27 weaken($weak_ctx);
47328014 28 my $params = ref($args[-1]) eq 'HASH' ? pop(@args) : {};
c575a0d7 29 if (ref($captures) eq 'ARRAY' || (!$captures && !scalar(@args))) { # FIXME BUG IN THIS LOGIC
30 $captures ||= [];
31 foreach my $name_or_sub ($stash_captures->flatten) {
32 unshift(@$captures, ref($name_or_sub) eq 'CODE' ? $name_or_sub->($weak_ctx) : $ctx->stash->{$name_or_sub});
33 }
47328014 34 $ctx->uri_for_action($action_or_private_path, $captures, @args, $params);
c575a0d7 35 }
36 };
37
38};
39
a42fd357 401;
41
42=head1 NAME
43
44CatalystX::UriFor::Curried -
45
46=head1 DESCRIPTION
47
48=head1 METHODS
49
50=head1 BUGS
51
52=head1 AUTHOR
53
54=head1 COPYRIGHT & LICENSE
55
56Copyright 2009 the above author(s).
57
58This sofware is free software, and is licensed under the same terms as perl itself.
59
60=cut
61