Map.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 22 / 22 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 97.3% 112 / 115 CC 14 SCAR 11.6
CC
14
Cov
99%
lookup
7.0
__ANON__
5.0
add_handler
2.0
CRAP 3.2
↑ Summary Map/Match.pm »
1 package Path::Map;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Path::Map - map paths to handlers
9  
10 =head1 VERSION
11  
12 0.04
13  
14 =cut
15  
16 2 our $VERSION = '0.04';
17  
18 =head1 SYNOPSIS
19  
20 my $mapper = Path::Map->new(
21 '/x/y/z' => 'XYZ',
22 '/a/b/c' => 'ABC',
23 '/a/b' => 'AB',
24  
25 '/date/:year/:month/:day' => 'Date',
26  
27 # Every path beginning 'SEO' is mapped the same.
28 '/seo/*' => 'slurpy',
29 );
30  
31 if (my $match = $mapper->lookup('/date/2013/12/25')) {
32 # $match->handler is 'Date'
33 # $match->variables is { year => 2012, month => 12, day => 25 }
34 }
35  
36 # Add more mappings later
37 $mapper->add_handler($path => $target)
38  
39 =head1 DESCRIPTION
40  
41 This class maps paths to handlers. The paths can contain variable path
42 segments, which match against any incoming path segment, where the matching
43 segments are saved as named variables for later retrieval.
44  
45 Note that the handlers being mapped to can be any arbitrary data, not just
46 strings as illustrated in the synopsis.
47  
48 =head2 Comparison with Path::Router
49  
50 This class fulfills some of the same jobs as L<Path::Router>, with slightly
51 different design goals. Broadly speaking, Path::Map is a lighter, faster,
52 but less featureful version of Path::Router.
53  
54 I've listed a few points of difference here to help highlight the pros and
55 cons of each class.
56  
57 =over
58  
59 =item Speed
60  
61 The main goal for Path::Map is lookup speed. Path::Router uses regexes to
62 do lookups, but Path::Map uses hash lookups. Path::Map seems to be at
63 least an order of magnitude faster based on my benchmarks, and performance
64 doesn't degrade with the number of routes that are added. The main source of
65 performance degradation for Path::Map is path I<depth>, Path::Router
66 degrades less with depth but more with width.
67  
68 This approach also means that the order in which routes are added makes no
69 difference to Path::Map.
70  
71 =item Reversibility
72  
73 Path::Router has a specific aim of being reversible. That is to say you can
74 construct a path from a set of parameters. Path::Map does not currently
75 have this ability, patches welcome!
76  
77 =item Validation
78  
79 Path::Map has no built-in ability to validate path variables in any way.
80 Obviously validation can be done externally after the fact, but that doesn't
81 allow for the more complex routing rules possible in Path::Router.
82  
83 In other words, it's not possible for Path::Map to differentiate two path
84 templates which differ only in the variable segments (e.g. C<< /blog/:name >>
85 vs C<< /blog/:id >> where C<id> matches C<\d+> and C<name> matches C<\D+>).
86  
87 =item Dependencies
88  
89 Path::Map has a very small dependency chain, whereas Path::Router is based
90 on L<Moose>, so has a relatively high dependency footprint. If you're already
91 using Moose, there's obviously no additional cost in using Path::Router.
92  
93 =back
94  
95 =cut
96  
97 2 ▶use List::Util qw( reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use List::MoreUtils qw( uniq natatime );
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 2 ▶use Path::Map::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 =head1 METHODS
103  
104 =head2 new
105  
106 $mapper = $class->new(@pairs)
107  
108 The constructor.
109  
110 Takes an even-sized list and passes each pair to L</add_handler>.
111  
112 =cut
113  
114 16 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
115 16 my $class = shift;
116 16 my $self = bless {}, $class;
117  
118 16 my $iterator = natatime 2, @_;
119 16 ▶ while (my @pair = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pair) = &$iterator()) 6 16 22
120 6 $self->add_handler(@pair);
121 }
122  
123 16 return $self;
124 }
125  
126 =head2 add_handler
127  
128 $mapper->add_handler($path_template, $handler)
129  
130 Adds a single item to the mapping.
131  
132 The path template should be a string comprising slash-delimited path segments,
133 where a path segment may contain any character other than the slash. Any
134 segment beginning with a colon (C<:>) denotes a mandatory named variable.
135 Empty segments, including those implied by leading or trailing slashes are
136 ignored.
137  
138 For example, these are all identical path templates:
139  
140 /a/:var/b
141 a/:var/b/
142 //a//:var//b//
143  
144 The order in which these templates are added has no bearing on the lookup,
145 except that later additions with identical templates overwrite earlier ones.
146  
147 Templates containing a segment consisting entirely of C<'*'> match instantly
148 at that point, with all remaining segments assigned to the C<values> of the
149 match as normal, but without any variable names. Any remaining segments in the
150 template are ignored, so it only makes sense for the wildcard to be the last
151 segment.
152  
153 my $map = Path::Map->new('foo/:foo/*' => 'Something');
154 my match = $map->lookup('foo/bar/baz/qux');
155 $match->variables; # { foo => 'bar' }
156 $match->values; # [ qw( bar baz qux ) ]
157  
158 =cut
159  
160 7 ▶sub add_handler {
Subroutine add_handler: called CC=2 SCAR=6.9
Pod documented
161 7 my ($self, $path, $handler) = @_;
162 7 my $class = ref $self;
163  
164 7 my @parts = $self->_tokenise_path($path);
165 7 my (@vars, $slurpy);
166 22 ▶ my $mapper = reduce {
Subroutine __ANON__: called CC=5 SCAR=16.1
167 22 ▶ $b =~ s{^:(.*)}{/} and push @vars, $1;
Branch: 1 branch
BranchTrueFalseTotal
$b =~ s[^:(.*)][/] and push @vars, $1 8 14 22
168 22 ▶ $b eq '*' and $slurpy = 1;
Branch: 1 branch
BranchTrueFalseTotal
$b eq "*" and $slurpy = 1 3 19 22
169 22 ▶ $slurpy ? $a : $a->_map->{$b} ||= $class->new;
Branch: 1 branch
BranchTrueFalseTotal
$slurpy ? : 4 18 22
Condition66%2/3
$slurpy ? $a : $a->_map->{$b} ||= $class->new
l!l&&r!l&&!r
9130
MC/DC0%0/2
$slurpy ? $a : $a->_map->{$b} ||= $class->new
$slurpy ? $a : $a->_map->{$b} $class->new
Truth table66%2/3
A ||= B
A= $slurpy ? $a : $a->_map->{$b}B= $class->new
ABresult
1X1
011
000
170 7 } $self, @parts;
171  
172 7 $mapper->_set_target($handler);
173 7 $mapper->_set_variables(\@vars);
174 7 ▶ $mapper->_set_slurpy if $slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $slurpy 3 4 7
175  
176 7 return;
177 }
178  
179 =head2 lookup
180  
181 $match = $mapper->lookup($path)
182  
183 Returns a L<Path::Map::Match> object if the path matches a known path
184 template, C<undef> otherwise.
185  
186 The two main methods on the match object are:
187  
188 =over
189  
190 =item handler
191  
192 The handler that was matched, identical to whatever was originally passed to
193 L</add_handler>.
194  
195 =item variables
196  
197 The named path variables as a hashref.
198  
199 =back
200  
201 =cut
202  
203 16 ▶sub lookup {
Subroutine lookup: called CC=7 SCAR=19.5
Pod documented
204 16 my ($mapper, $path) = @_;
205  
206 16 my @parts = $mapper->_tokenise_path($path);
207 16 my @values;
208 my $slurpy_match;
209  
210 16 while () {
211 66 ▶ if ($mapper->_is_slurpy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapper->_is_slurpy) 6 60 66
212 6 $slurpy_match = Path::Map::Match->new(
213 mapper => $mapper,
214 values => [ @values, @parts ],
215 );
216 }
217  
218 66 ▶ if (my $segment = shift @parts) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $segment = shift @parts) { } 54 12 66
elsif (defined $mapper->_target) { } 9 3 12
219 54 my $map = $mapper->_map;
220  
221 54 my $next;
222 54 ▶ if ($next = $map->{$segment}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($next = $map->{$segment}) { } 20 34 54
elsif ($next = $map->{'/'}) { } 30 4 34
elsif ($slurpy_match) { } 3 1 4
223 # Nothing
224 }
225 elsif ($next = $map->{'/'}) {
226 30 push @values, $segment;
227 }
228 elsif ($slurpy_match) {
229 3 return $slurpy_match;
230 }
231 else {
232 1 return undef;
233 }
234  
235 50 $mapper = $next;
236 }
237 elsif (defined $mapper->_target) {
238 9 return Path::Map::Match->new(
239 mapper => $mapper,
240 values => \@values
241 );
242 }
243 else {
244 3 return undef;
245 }
246 }
247 }
248  
249 =head2 handlers
250  
251 @handlers = $mapper->handlers()
252  
253 Returns all of the handlers in no particular order.
254  
255 =cut
256  
257 9 ▶sub handlers {
Subroutine handlers: called CC=1 SCAR=0.0
Pod documented
258 9 my $self = shift;
259  
260 return uniq(
261 9 grep defined, $self->_target, map $_->handlers, values %{ $self->_map }
262 );
263 }
264  
265 23 ▶sub _tokenise_path {
Subroutine _tokenise_path: called CC=1 SCAR=0.0
266 23 my ($self, $path) = @_;
267  
268 23 return grep length, split '/', $path;
269 }
270  
271 66 ▶sub _is_slurpy {
Subroutine _is_slurpy: called CC=1 SCAR=0.0
272 66 return defined $_[0]->{slurpy};
273 }
274  
275 3 ▶sub _set_slurpy { $_[0]->{slurpy} = 1 }
Subroutine _set_slurpy: called CC=1 SCAR=0.0
276  
277 81 ▶sub _map { $_[0]->{map} ||= {} }
Subroutine _map: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]{"map"} ||= {}
l!l
6615
MC/DC100%1/1
$_[0]{"map"} ||= {}
$_[0]{"map"}
Truth table100%2/2
A
A= $_[0]{"map"}
Aresult
11
00
278  
279 33 ▶sub _target { $_[0]->{target} }
Subroutine _target: called CC=1 SCAR=0.0
280 7 ▶sub _set_target { $_[0]->{target} = $_[1] }
Subroutine _set_target: called CC=1 SCAR=0.0
281  
282 12 ▶sub _variables { $_[0]->{vars} }
Subroutine _variables: called CC=1 SCAR=0.0
283 7 ▶sub _set_variables { $_[0]->{vars} = $_[1] }
Subroutine _set_variables: called CC=1 SCAR=0.0
284  
285 =head1 SEE ALSO
286  
287 L<Path::Router>
288  
289 =head1 AUTHOR
290  
291 Matt Lawrence E<lt>mattlaw@cpan.orgE<gt>
292  
293 =head1 COPYRIGHT
294  
295 This library is free software; you can redistribute it and/or modify it under
296 the same terms as Perl itself.
297  
298 =cut
299  
300 2 1;
↑ Summary Map/Match.pm »