| 1 | package Dancer::Plugin::Chain; | ||||||||||||||
| 2 | BEGIN { | ||||||||||||||
| 3 | 2 | ▶ | $Dancer::Plugin::Chain::AUTHORITY = 'cpan:YANICK'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | } | ||||||||||||||
| 5 | # ABSTRACT: Chained actions for Dancer | ||||||||||||||
| 6 | 2 | $Dancer::Plugin::Chain::VERSION = '0.1.0'; | |||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 2 | ▶ | use Dancer ':syntax'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use Dancer::Plugin; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | register chain => sub { | ||||||||||||||
| 14 | 7 | ▶ | my $link = Dancer::Plugin::Chain::Link->new( args => [ @_ ] ); | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 7 | ▶ | return wantarray ? $link->as_route : $link; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | 2 | }; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | register_plugin; | |||||||||||||
| 20 | |||||||||||||||
| 21 | package | ||||||||||||||
| 22 | Dancer::Plugin::Chain::Link; | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | has "path_segments" => ( | ||||||||||||||
| 27 | traits => [ qw/ Array /], | ||||||||||||||
| 28 | isa => 'ArrayRef', | ||||||||||||||
| 29 | is => 'ro', | ||||||||||||||
| 30 | 7 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 2 | handles => { | |||||||||||||
| 32 | add_to_path => 'push', | ||||||||||||||
| 33 | all_path_segments => 'elements' | ||||||||||||||
| 34 | }, | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | |||||||||||||||
| 37 | sub path { | ||||||||||||||
| 38 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 39 | 3 | return join '', $self->all_path_segments; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | has code_blocks => ( | ||||||||||||||
| 43 | traits => [ qw/ Array /], | ||||||||||||||
| 44 | isa => 'ArrayRef', | ||||||||||||||
| 45 | is => 'ro', | ||||||||||||||
| 46 | 7 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 2 | handles => { | |||||||||||||
| 48 | add_to_code => 'push', | ||||||||||||||
| 49 | all_code_blocks => 'elements' | ||||||||||||||
| 50 | }, | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | sub code { | ||||||||||||||
| 54 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 3 | my @code = $self->all_code_blocks; | |||||||||||||
| 57 | return sub { | ||||||||||||||
| 58 | 3 | ▶ | my $result; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 59 | 3 | $result = $_->(@_) for @code; | |||||||||||||
| 60 | 3 | return $result; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | 3 | } | |||||||||||||
| 63 | |||||||||||||||
| 64 | sub BUILD { | ||||||||||||||
| 65 | 7 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 66 | 7 | my @args = @{ $_[0]{args} }; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 7 | my $code; | |||||||||||||
| 69 | 7 | ▶ | $code = pop @args if ref $args[-1] eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | 7 | for my $segment ( @args ) { | |||||||||||||
| 72 | 16 | ▶ | if ( ref $segment eq __PACKAGE__ ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 73 | 8 | $self->add_to_path( $segment->all_path_segments ); | |||||||||||||
| 74 | 8 | $self->add_to_code( $segment->all_code_blocks ); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | elsif( ref $segment eq 'CODE' ) { | ||||||||||||||
| 77 | 2 | $self->add_to_code($segment); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | else { | ||||||||||||||
| 80 | 6 | $self->add_to_path( $segment ); | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 7 | ▶ | $self->add_to_code($code) if $code; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | sub as_route { | ||||||||||||||
| 88 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
as_route: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | 3 | return ( $self->path, $self->code ); | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 2 | 1; | |||||||||||||
| 96 | |||||||||||||||
| 97 | __END__ | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =pod | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head1 NAME | ||||||||||||||
| 102 | |||||||||||||||
| 103 | Dancer::Plugin::Chain - Chained actions for Dancer | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 VERSION | ||||||||||||||
| 106 | |||||||||||||||
| 107 | version 0.1.0 | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 SYNOPSIS | ||||||||||||||
| 110 | |||||||||||||||
| 111 | use Dancer; | ||||||||||||||
| 112 | use Dancer::Plugin::Chain; | ||||||||||||||
| 113 | |||||||||||||||
| 114 | my $country = chain '/country/:country' => sub { | ||||||||||||||
| 115 | # silly example. Typically much more work would | ||||||||||||||
| 116 | # go on in here | ||||||||||||||
| 117 | var 'site' => param('country'); | ||||||||||||||
| 118 | }; | ||||||||||||||
| 119 | |||||||||||||||
| 120 | my $event = chain '/event/:event' => sub { | ||||||||||||||
| 121 | var 'event' => param('event'); | ||||||||||||||
| 122 | }; | ||||||||||||||
| 123 | |||||||||||||||
| 124 | # will match /country/usa/event/yapc | ||||||||||||||
| 125 | get chain $country, $event, '/schedule' => sub { | ||||||||||||||
| 126 | return sprintf "schedule of %s in %s\n", map { var $_ } | ||||||||||||||
| 127 | qw/ event site /; | ||||||||||||||
| 128 | }; | ||||||||||||||
| 129 | |||||||||||||||
| 130 | my $continent = chain '/continent/:continent' => sub { | ||||||||||||||
| 131 | var 'site' => param('continent'); | ||||||||||||||
| 132 | }; | ||||||||||||||
| 133 | |||||||||||||||
| 134 | my $continent_event = chain $continent, $event; | ||||||||||||||
| 135 | |||||||||||||||
| 136 | # will match /continent/europe/event/yapc | ||||||||||||||
| 137 | get chain $continent_event, '/schedule' => sub { | ||||||||||||||
| 138 | return sprintf "schedule of %s in %s\n", map { var $_ } qw/ event site /; | ||||||||||||||
| 139 | }; | ||||||||||||||
| 140 | |||||||||||||||
| 141 | # will match /continent/asia/country/japan/event/yapc | ||||||||||||||
| 142 | # and will do special munging in-between! | ||||||||||||||
| 143 | |||||||||||||||
| 144 | get chain $continent, | ||||||||||||||
| 145 | sub { var temp => var 'site' }, | ||||||||||||||
| 146 | $country, | ||||||||||||||
| 147 | sub { | ||||||||||||||
| 148 | var 'site' => join ', ', map { var $_ } qw/ site temp / | ||||||||||||||
| 149 | }, | ||||||||||||||
| 150 | $event, | ||||||||||||||
| 151 | '/schedule' | ||||||||||||||
| 152 | => sub { | ||||||||||||||
| 153 | return sprintf "schedule of %s in %s\n", map { var $_ } | ||||||||||||||
| 154 | qw/ event site /; | ||||||||||||||
| 155 | }; | ||||||||||||||
| 156 | |||||||||||||||
| 157 | =head1 DESCRIPTION | ||||||||||||||
| 158 | |||||||||||||||
| 159 | Implementation of Catalyst-like chained routes. | ||||||||||||||
| 160 | |||||||||||||||
| 161 | The plugin exports a single keyword, C<chain>, which creates the chained | ||||||||||||||
| 162 | routes. | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =head2 KNOWN CAVEATS | ||||||||||||||
| 165 | |||||||||||||||
| 166 | The plugin does not support C<prefix> yet, and only support string-based urls | ||||||||||||||
| 167 | (so no regexes). | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =head1 EXPORTED FUNCTIONS | ||||||||||||||
| 170 | |||||||||||||||
| 171 | =head2 chain @chain_items, $coderef | ||||||||||||||
| 172 | |||||||||||||||
| 173 | Create a chain out of the items provided, and assign it the final action coderef. | ||||||||||||||
| 174 | |||||||||||||||
| 175 | Each chain item can be | ||||||||||||||
| 176 | a string representing a path segment, a previously defined chain or an | ||||||||||||||
| 177 | anonymous function. The chain's final path and action will be the aggregate of | ||||||||||||||
| 178 | its parts. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | For example, the final route declaration of the SYNOPSIS, | ||||||||||||||
| 181 | |||||||||||||||
| 182 | get chain $continent, | ||||||||||||||
| 183 | sub { var temp => var 'site' }, | ||||||||||||||
| 184 | $country, | ||||||||||||||
| 185 | sub { | ||||||||||||||
| 186 | var 'site' => join ', ', map { var $_ } qw/ site temp / | ||||||||||||||
| 187 | }, | ||||||||||||||
| 188 | $event, | ||||||||||||||
| 189 | '/schedule' | ||||||||||||||
| 190 | => sub { | ||||||||||||||
| 191 | return sprintf "schedule of %s in %s\n", map { var $_ } | ||||||||||||||
| 192 | qw/ event site /; | ||||||||||||||
| 193 | }; | ||||||||||||||
| 194 | |||||||||||||||
| 195 | would be is equivalent to | ||||||||||||||
| 196 | |||||||||||||||
| 197 | get '/continent/:continent/country/:country/event/:event/schedule' => sub { | ||||||||||||||
| 198 | var 'site' => param('continent'); | ||||||||||||||
| 199 | var temp => var 'site'; | ||||||||||||||
| 200 | var 'site' => param('country'); | ||||||||||||||
| 201 | var 'site' => join ', ', map { var $_ } qw/ site temp / | ||||||||||||||
| 202 | var 'event' => param('event'); | ||||||||||||||
| 203 | |||||||||||||||
| 204 | return sprintf "schedule of %s in %s\n", map { var $_ } | ||||||||||||||
| 205 | qw/ event site /; | ||||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | In scalar context, C<chain> returns its underlying object. | ||||||||||||||
| 209 | In list context, it returns a route / action pair of values (). That's how it | ||||||||||||||
| 210 | can work transparently with C<get>, C<post> and friends. | ||||||||||||||
| 211 | |||||||||||||||
| 212 | # returns the object, that can be used to forge longer chains. | ||||||||||||||
| 213 | my $foo_chain = chain '/foo', sub { ... }; | ||||||||||||||
| 214 | |||||||||||||||
| 215 | # returns the pair that makes 'get' happy | ||||||||||||||
| 216 | get chain $foo_chain; | ||||||||||||||
| 217 | |||||||||||||||
| 218 | =head1 SEE ALSO | ||||||||||||||
| 219 | |||||||||||||||
| 220 | =over | ||||||||||||||
| 221 | |||||||||||||||
| 222 | =item * | ||||||||||||||
| 223 | |||||||||||||||
| 224 | Original blog entry: L<http://techblog.babyl.ca/entry/dancer-in-chains> | ||||||||||||||
| 225 | |||||||||||||||
| 226 | =item * | ||||||||||||||
| 227 | |||||||||||||||
| 228 | L<Dancer-Plugin-Dispatcher> | ||||||||||||||
| 229 | |||||||||||||||
| 230 | =back | ||||||||||||||
| 231 | |||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||
| 233 | |||||||||||||||
| 234 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||||||
| 235 | |||||||||||||||
| 236 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 237 | |||||||||||||||
| 238 | This software is copyright (c) 2014 by Yanick Champoux. | ||||||||||||||
| 239 | |||||||||||||||
| 240 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 241 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 242 | |||||||||||||||
| 243 | =cut | ||||||||||||||