Permalink
Browse files

update.

  • Loading branch information...
1 parent 5d44ca8 commit 57da59de8f9da08dc997b04d43ff690c2aaf8518 @yusukebe committed Jul 10, 2011
View
8 eg/MyApp/lib/MyApp/Controller/JSON.pm
@@ -1,8 +0,0 @@
-package MyApp::Controller::JSON;
-
-sub index {
- my ( $self, $c ) = @_;
- $c->render( { as => 'JSON' }, { message => $c->config->{message} } );
-}
-
-1;
View
17 eg/MyApp/lib/MyApp/Controller/Root.pm
@@ -1,21 +1,8 @@
package MyApp::Controller::Root;
sub index {
- my ( $self, $c ) = @_;
- $c->render('index', { message => $c->config->{message} } );
+ my $self = shift;
+ render( 'index', { message => $c->config->{message} } );
}
-sub hi {
- my ( $self, $c ) = @_;
- my $name = $c->req->param('name') || 'nanashi';
- $c->render('hi', { name => $name } );
-}
-
-sub redirect {
- my ($self, $c ) = @_;
- $c->redirect( $c->base );
-}
-
-sub error {}
-
1;
View
35 lib/Noe.pm
@@ -2,36 +2,45 @@ package Noe;
use strict;
use warnings;
use Router::Simple;
-use Noe::Util qw/dumper/;
+use Noe::Util qw//;
use Noe::Request;
use Noe::Response;
+use Plack::Util;
our $VERSION = '0.02';
-sub setup_route { die "This is abstract method: route" };
+sub setup_router { die "This is abstract method: setup_router" };
sub router {
return Router::Simple->new;
}
sub new {
my ( $class, %opt ) = @_;
my $self = bless \%opt, $class;
- $self->{_router} ||= $self->setup_route();
+ $self->{_router} ||= $self->setup_router();
return $self;
}
-sub app {
+sub psgi {
my $self = shift;
return sub {
my $env = shift;
my $route = $self->dispatch($env);
- dumper $route;
+ return $self->res_404 unless $route;
+ my $class = Plack::Util::load_class( "Controller::$route->{controller}", ref $self );
+ my $method = $route->{action};
+ my $code = $class->$method;
+ return $code;
};
}
+sub res_404 {
+ return [404, [ 'Content-Type' => 'text/plain' ], ['404 Not Found']];
+}
+
sub dispatch {
my ( $self, $env ) = @_;
- return $self->router->match($env);
+ return $self->{_router}->match($env);
}
1;
@@ -40,19 +49,7 @@ __END__
=head1 NAME
-Noe::Web
+Noe
=head1 SYNOPSIS
-in MyApp/Web.pm
-
- package MyApp::Web;
- use parent qw/Noe::Web/;
- 1;
-
-in myapp.psgi
-
- use MyApp::Web;
- my $web = MyApp::Web->new;
- $web->app;
-
View
9 lib/Noe/Controller.pm
@@ -0,0 +1,9 @@
+package Noe::Controller;
+use strict;
+use warnings;
+
+sub render {
+ return [200,[],['Hello']];
+}
+
+1;
View
9 lib/Noe/Util.pm
@@ -2,13 +2,6 @@ package Noe::Util;
use strict;
use warnings;
use base qw/Exporter/;
-
-our @EXPORT_OK = qw/dumper/;
-
-sub dumper {
- my ( $self, $data ) = @_;
- require YAML;
- return Dump $data;
-}
+our @EXPORT_OK = qw//;
1;
View
8 t/00_compile.t
@@ -1,4 +1,8 @@
use strict;
-use Test::More tests => 1;
+use Test::More;
-BEGIN { use_ok 'Noe' }
+BEGIN { use_ok 'Noe';
+ use_ok 'Noe::Util';
+ }
+
+done_testing;
View
9 t/100_myapp.t
@@ -3,7 +3,16 @@ use Test::More;
use FindBin;
use lib "$FindBin::Bin/lib/";
use MyApp;
+use Plack::Test;
+use HTTP::Request::Common qw/GET POST/;
my $app = MyApp->new;
+test_psgi $app->psgi, sub {
+ my $cb = shift;
+ my $res = $cb->( GET('/') );
+ is $res->code, 200;
+ diag $res->content;
+};
+
done_testing;
View
7 t/lib/MyApp.pm
@@ -1,4 +1,11 @@
package MyApp;
use base qw/Noe/;
+sub setup_router {
+ my $self = shift;
+ my $r = $self->router;
+ $r->connect( '/', { controller => 'Root', action => 'index' } );
+ return $r;
+}
+
1;
View
9 t/lib/MyApp/Controller/Root.pm
@@ -0,0 +1,9 @@
+package MyApp::Controller::Root;
+use base 'Noe::Controller';
+
+sub index {
+ my $self = shift;
+ $self->render('index');
+}
+
+1;

0 comments on commit 57da59d

Please sign in to comment.