Permalink
Browse files

filename munger to allow custom fiddling

  • Loading branch information...
1 parent c44b254 commit 9391cd653e936dcb71903c5dd58a1234d280418c @rjbs committed Jul 13, 2011
Showing with 30 additions and 8 deletions.
  1. +11 −5 lib/Router/Dumb/Dumber.pm
  2. +19 −3 t/basic.t
  3. 0 templates/pages/images/INDEX
View
@@ -9,12 +9,18 @@ use Moose::Util::TypeConstraints qw(find_type_constraint);
use namespace::autoclean;
-has root_dir => (
+has simple_root => (
is => 'ro',
isa => 'Str',
required => 1,
);
+has simple_munger => (
+ reader => '_simple_munger',
+ isa => 'CodeRef',
+ default => sub { sub { $_[1] } },
+);
+
has extras_file => (
is => 'ro',
isa => 'Str',
@@ -29,13 +35,13 @@ sub BUILD {
sub _build_routes {
my ($self) = @_;
- my $root = $self->root_dir;
- my @files = `find $root -type f`;
+ my $dir = $self->simple_root;
+ my @files = `find $dir -type f`;
chomp @files;
for my $file (@files) {
my $path = $file =~ s{/INDEX$}{/}gr;
- $path =~ s{$root}{};
+ $path =~ s{$dir}{};
$path =~ s{^/}{};
my @parts = split m{/}, $path;
@@ -47,7 +53,7 @@ sub _build_routes {
my $route = Router::Dumb::Route->new({
parts => \@parts,
- target => $file,
+ target => $self->_simple_munger->( $self, $file ),
});
$self->add_route($route);
View
@@ -4,11 +4,17 @@ use Test::More;
use Test::Deep qw(cmp_deeply listmethods);
use Moose::Util::TypeConstraints qw(find_type_constraint);
+use Path::Class;
use Router::Dumb::Dumber;
my $r = Router::Dumb::Dumber->new({
- root_dir => 'templates/pages',
- extras_file => 'eg/extras',
+ simple_root => 'templates/pages',
+ simple_munger => sub {
+ my ($self, $filename) = @_;
+ dir('pages')->file( file($filename)->relative($self->simple_root) )
+ ->stringify;
+ },
+ extras_file => 'eg/extras',
});
# Canonicalize hash. This is stupid. I need it because Test::Deep doesn't yet
@@ -29,10 +35,20 @@ $r->add_route(
);
my @tests = (
+ '/' => {
+ target => [ 'pages/INDEX' ],
+ matches => _CH(),
+ },
+
+ '/images' => {
+ target => [ 'pages/images/INDEX' ],
+ matches => _CH(),
+ },
+
'/legal' => undef,
'/legal/privacy' => {
- target => [ 'templates/pages/legal/privacy' ],
+ target => [ 'pages/legal/privacy' ],
matches => _CH(),
},
No changes.

0 comments on commit 9391cd6

Please sign in to comment.