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