Permalink
Browse files

update Noe::Dispatcher

  • Loading branch information...
1 parent d7f0662 commit c305fd18820d09e12a720aff584e43a292588181 @yusukebe committed Mar 16, 2010
Showing with 80 additions and 8 deletions.
  1. +1 −1 Makefile.PL
  2. +37 −5 lib/Noe/Dispatcher.pm
  3. +36 −0 t/30_dispatch.t
  4. +6 −2 t/lib/TestApp/Dispatcher.pm
View
2 Makefile.PL
@@ -8,7 +8,7 @@ requires 'Template';
requires 'IO::File';
requires 'Path::Class';
requires 'YAML';
-requires 'HTTPx::Dispatcher';
+requires 'HTTPx::Dispatcher' => 0.07;
requires 'Text::MicroTemplate::Extended';
recommends 'JSON::XS';
View
42 lib/Noe/Dispatcher.pm
@@ -1,11 +1,43 @@
package Noe::Dispatcher;
use strict;
use warnings;
-use HTTPx::Dispatcher;
-our @EXPORT = qw/connect match/;
+use base qw/Exporter/;
+use Scalar::Util qw/blessed/;
+use Carp;
+use HTTPx::Dispatcher::Rule;
+our @EXPORT = qw/connect match get post/;
-# XXX
-*match = \&HTTPx::Dispatcher::match;
-*connect = \&HTTPx::Dispatcher::connect;
+my $rules;
+
+sub _push {
+ push @$rules, HTTPx::Dispatcher::Rule->new(@_)
+}
+
+sub connect {
+ _push( @_ );
+}
+
+sub get {
+ my @args = @_;
+ $args[1]->{conditions} = { method => 'GET' };
+ _push( @args );
+}
+
+sub post {
+ my @args = @_;
+ $args[1]->{conditions} = { method => 'POST' };
+ _push( @args );
+}
+
+# stolen from HTTPx::Dispatcher
+sub match {
+ my ( $class, $req ) = @_;
+ for my $rule ( @$rules ) {
+ if ( my $result = $rule->match($req) ) {
+ return $result;
+ }
+ }
+ return;
+}
1;
View
36 t/30_dispatch.t
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use lib 't/lib';
+use Test::More;
+use Plack::Request;
+use TestApp::Dispatcher;
+
+use_ok('Noe::Dispatcher');
+
+{
+ my $req =
+ Plack::Request->new(
+ { PATH_INFO => '/', REQUEST_METHOD => 'GET', } );
+ my $rule = TestApp::Dispatcher->match($req);
+ is_deeply( $rule,
+ { controller => 'Root', args => {}, action => 'index' } );
+}
+
+{
+ my $req =
+ Plack::Request->new(
+ { PATH_INFO => '/comment', REQUEST_METHOD => 'GET', } );
+ my $rule = TestApp::Dispatcher->match($req);
+ is_deeply( $rule,
+ { controller => 'Comment', args => {}, action => 'show' } );
+}
+
+{
+ my $req =
+ Plack::Request->new( { PATH_INFO => '/comment', REQUEST_METHOD => 'POST', } );
+ my $rule = TestApp::Dispatcher->match( $req );
+ is_deeply( $rule,
+ { controller => 'Comment', args => {}, action => 'post' } );
+}
+
+done_testing;
View
8 t/lib/TestApp/Dispatcher.pm
@@ -1,4 +1,8 @@
package TestApp::Dispatcher;
-use HTTPx::Dispatcher;
-connect '' => {controller => 'Root', action => 'root'};
+use Noe::Dispatcher;
+
+connect '' => { controller => 'Root', action => 'index' };
+get '/comment' => { controller => 'Comment', action => 'show' };
+post '/comment' => { controller => 'Comment', action => 'post' };
+
1;

0 comments on commit c305fd1

Please sign in to comment.