Browse files

break Dumber into two helpers

  • Loading branch information...
1 parent ce878a2 commit 971dca848d651b1191cfb13ad29f8f249914ba20 @rjbs committed Jul 14, 2011
Showing with 120 additions and 113 deletions.
  1. +0 −106 lib/Router/Dumb/Dumber.pm
  2. +51 −0 lib/Router/Dumb/Helper/FileMapper.pm
  3. +56 −0 lib/Router/Dumb/Helper/RouteFile.pm
  4. +13 −7 t/basic.t
View
106 lib/Router/Dumb/Dumber.pm
@@ -1,106 +0,0 @@
-use 5.14.0;
-package Router::Dumb::Dumber;
-use Moose;
-extends 'Router::Dumb';
-
-use File::Find::Rule;
-use Router::Dumb::Route;
-
-use Moose::Util::TypeConstraints qw(find_type_constraint);
-
-use namespace::autoclean;
-
-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',
- predicate => 'has_extras_file',
-);
-
-sub BUILD {
- my ($self) = @_;
- $self->_build_routes;
-}
-
-sub _build_routes {
- my ($self) = @_;
-
- my $dir = $self->simple_root;
- my @files = File::Find::Rule->file->in($dir);
-
- for my $file (@files) {
- my $path = $file =~ s{/INDEX$}{/}gr;
- $path =~ s{$dir}{};
- $path =~ s{^/}{};
-
- my @parts = split m{/}, $path;
-
- confess "can't use placeholder-like name in route files"
- if grep {; /^:/ } @parts;
-
- confess "can't use asterisk in file names" if grep {; $_ eq '*' } @parts;
-
- my $route = Router::Dumb::Route->new({
- parts => \@parts,
- target => $self->_simple_munger->( $self, $file ),
- });
-
- $self->add_route($route);
- }
-
- if ($self->has_extras_file) {
- $self->_add_routes_from_file( $self->extras_file );
- }
-}
-
-sub _add_routes_from_file {
- my ($self, $file) = @_;
-
- my @lines = `cat $file`;
- chomp @lines;
-
- # ignore comments, blanks
- @lines = grep { /\S/ }
- map { s/#.*\z//r } @lines;
-
- my $curr;
-
- for my $i (0 .. $#lines) {
- my $line = $lines[$i];
-
- if ($line =~ /^\s/) {
- confess "indented line found out of context of a route" unless $curr;
- confess "couldn't understand line <$line>"
- unless my ($name, $type) = $line =~ /\A\s+(\S+)\s+isa\s+(\S+)\s*\z/;
-
- $curr->{constraints}->{$name} = find_type_constraint($type);
- } else {
- my ($path, $target) = split /\s*=>\s*/, $line;
- s{^/}{} for $path, $target;
- my @parts = split m{/}, $path;
-
- $curr = {
- parts => \@parts,
- target => $target,
- };
- }
-
- if ($curr and ($i == $#lines or $lines[ $i + 1 ] =~ /^\S/)) {
- $self->add_route( Router::Dumb::Route->new($curr) );
- undef $curr;
- }
- }
-}
-
-1;
View
51 lib/Router/Dumb/Helper/FileMapper.pm
@@ -0,0 +1,51 @@
+use 5.14.0;
+package Router::Dumb::Helper::FileMapper;
+use Moose;
+
+use File::Find::Rule;
+use Router::Dumb::Route;
+
+use Moose::Util::TypeConstraints qw(find_type_constraint);
+
+use namespace::autoclean;
+
+has root => (
+ is => 'ro',
+ isa => 'Str',
+ required => 1,
+);
+
+has target_munger => (
+ reader => '_target_munger',
+ isa => 'CodeRef',
+ default => sub { sub { $_[1] } },
+);
+
+sub add_routes_to {
+ my ($self, $router) = @_;
+
+ my $dir = $self->root;
+ my @files = File::Find::Rule->file->in($dir);
+
+ for my $file (@files) {
+ my $path = $file =~ s{/INDEX$}{/}gr;
+ $path =~ s{$dir}{};
+ $path =~ s{^/}{};
+
+ my @parts = split m{/}, $path;
+
+ confess "can't use placeholder-like name in route files"
+ if grep {; /^:/ } @parts;
+
+ confess "can't use asterisk in file names" if grep {; $_ eq '*' } @parts;
+
+ my $route = Router::Dumb::Route->new({
+ parts => \@parts,
+ target => $self->_target_munger->( $self, $file ),
+ });
+
+ $router->add_route($route);
+ }
+}
+
+1;
View
56 lib/Router/Dumb/Helper/RouteFile.pm
@@ -0,0 +1,56 @@
+use 5.14.0;
+package Router::Dumb::Helper::RouteFile;
+use Moose;
+
+use Router::Dumb::Route;
+
+use Moose::Util::TypeConstraints qw(find_type_constraint);
+
+use namespace::autoclean;
+
+has filename => (is => 'ro', isa => 'Str', required => 1);
+
+sub add_routes_to {
+ my ($self, $router) = @_;
+
+ my $file = $self->filename;
+
+ my @lines;
+ {
+ open my $fh, '<', $file or die "can't open $file for reading: $!";
+
+ # ignore comments, blanks
+ @lines = grep { /\S/ }
+ map { chomp; s/#.*\z//r } <$fh>
+ }
+
+ my $curr;
+
+ for my $i (0 .. $#lines) {
+ my $line = $lines[$i];
+
+ if ($line =~ /^\s/) {
+ confess "indented line found out of context of a route" unless $curr;
+ confess "couldn't understand line <$line>"
+ unless my ($name, $type) = $line =~ /\A\s+(\S+)\s+isa\s+(\S+)\s*\z/;
+
+ $curr->{constraints}->{$name} = find_type_constraint($type);
+ } else {
+ my ($path, $target) = split /\s*=>\s*/, $line;
+ s{^/}{} for $path, $target;
+ my @parts = split m{/}, $path;
+
+ $curr = {
+ parts => \@parts,
+ target => $target,
+ };
+ }
+
+ if ($curr and ($i == $#lines or $lines[ $i + 1 ] =~ /^\S/)) {
+ $router->add_route( Router::Dumb::Route->new($curr) );
+ undef $curr;
+ }
+ }
+}
+
+1;
View
20 t/basic.t
@@ -5,17 +5,23 @@ use Test::Deep qw(cmp_deeply listmethods);
use Moose::Util::TypeConstraints qw(find_type_constraint);
use Path::Class;
-use Router::Dumb::Dumber;
+use Router::Dumb;
+use Router::Dumb::Helper::FileMapper;
+use Router::Dumb::Helper::RouteFile;
-my $r = Router::Dumb::Dumber->new({
- simple_root => 'templates/pages',
- simple_munger => sub {
+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->simple_root) )
+ dir('pages')->file( file($filename)->relative($self->root) )
->stringify;
},
- extras_file => 'eg/extras',
-});
+})->add_routes_to($r);
+
+Router::Dumb::Helper::RouteFile->new({ filename => 'eg/extras' })
+ ->add_routes_to($r);
# Canonicalize hash. This is stupid. I need it because Test::Deep doesn't yet
# have a way to do pairwise comparison. -- rjbs, 2011-07-13

0 comments on commit 971dca8

Please sign in to comment.