Initial sketch from work code
[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);
28 if (ref($captures) eq 'ARRAY' || (!$captures && !scalar(@args))) { # FIXME BUG IN THIS LOGIC
29 $captures ||= [];
30 foreach my $name_or_sub ($stash_captures->flatten) {
31 unshift(@$captures, ref($name_or_sub) eq 'CODE' ? $name_or_sub->($weak_ctx) : $ctx->stash->{$name_or_sub});
32 }
33 $ctx->uri_for_action($action_or_private_path, $captures, @args);
34 }
35 };
36
37};
38
a42fd357 391;
40
41=head1 NAME
42
43CatalystX::UriFor::Curried -
44
45=head1 DESCRIPTION
46
47=head1 METHODS
48
49=head1 BUGS
50
51=head1 AUTHOR
52
53=head1 COPYRIGHT & LICENSE
54
55Copyright 2009 the above author(s).
56
57This sofware is free software, and is licensed under the same terms as perl itself.
58
59=cut
60