NAME
Router::Dumb::Helper::FileMapper - something to build routes out of a dumb tree of files
VERSION
version 0.006
OVERVIEW
The FileMapper helper looks over a tree of files and adds routes to a Router::Dumb object based on those files.
For example, imagine the following file hierarchy:
templates
templates/pages
templates/pages/help
templates/pages/images
templates/pages/images/INDEX
templates/pages/INDEX
templates/pages/legal
templates/pages/legal/privacy
templates/pages/legal/tos
With the following code...
use Path::Class qw(dir);
my $r = Router::Dumb->new;
Router::Dumb::Helper::FileMapper->new({
root => 'templates/pages',
target_munger => sub {
my ($self, $filename) = @_;
dir('pages')->file( file($filename)->relative($self->root) )
->stringify;
},
})->add_routes_to($r);
...the router will have a route so that:
$r->route( '/legal/privacy' )->target eq 'pages/legal/privacy';
These routes never have placeholders, and if files in the tree have colons at the beginning of their names, an exception will be thrown. Similarly, slurpy routes will never be added, and files named *
are forbidden.
Files named INDEX are special: they cause a route for the directory's name to exist.
PERL VERSION
This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.
ATTRIBUTES
root
This is the name of the root directory to scan when adding routes.
target_munger
This attribute (which has a default no-op value) must be a coderef. It is called like a method, with the first non-self argument being the file responsible for the route. It should return the target for the route to be added.
parts_munger
This attribute (which has a default no-op value) must be a coderef. It is called like a method, with the first non-self argument being an arrayref of the path components of the file responsible for the route. It should return the parts for the route to be added.
METHODS
add_routes_to
$helper->add_routes_to( $router, \%arg );
This message tells the helper to scan its directory root and add routes to the given router. The helper can be used over and over.
Valid arguments are:
ignore_conflicts - if true, trying adding an existing route will be ignored,
rather than fail
AUTHOR
Ricardo Signes <cpan@semiotic.systems>
COPYRIGHT AND LICENSE
This software is copyright (c) 2022 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.