Skip to content

Commit

Permalink
merge from instance-and-declare branch
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.coderepos.org/share/lang/perl/HTTPx-Dispatcher/trunk@25653 d0d07461-0603-4401-acd4-de1884942a52
  • Loading branch information
masaki committed Dec 1, 2008
1 parent 058e2fd commit 3e38538
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 32 deletions.
41 changes: 21 additions & 20 deletions lib/HTTPx/Dispatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ package HTTPx::Dispatcher;
use strict;
use warnings;
use 5.00800;
use base qw/Class::Accessor::Fast/;
our $VERSION = '0.05';
use HTTPx::Dispatcher::Rule;
use Scalar::Util qw/blessed/;
use Carp;
use Exporter 'import';

our @EXPORT = qw/connect match uri_for/;
__PACKAGE__->mk_accessors(qw/rules/);

my $rules;

sub connect {
my $pkg = caller(0);
my @args = @_;
sub new {
my $class = shift;
return $class->SUPER::new({ rules => [] });
}

push @{ $rules->{$pkg} }, HTTPx::Dispatcher::Rule->new(@args);
sub add_rule {
my ( $self, @args ) = @_;
push @{ $self->rules }, HTTPx::Dispatcher::Rule->new(@args);
}

sub match {
my ( $class, $req ) = @_;
my ( $self, $req ) = @_;
croak "request required" unless blessed $req;

for my $rule ( @{ $rules->{$class} } ) {
for my $rule ( @{ $self->rules } ) {
if ( my $result = $rule->match($req) ) {
return $result;
}
Expand All @@ -32,9 +33,9 @@ sub match {
}

sub uri_for {
my ( $class, @args ) = @_;
my ( $self, @args ) = @_;

for my $rule ( @{ $rules->{$class} } ) {
for my $rule ( @{ $self->rules } ) {
if ( my $result = $rule->uri_for(@args) ) {
return $result;
}
Expand All @@ -54,21 +55,19 @@ HTTPx::Dispatcher - the uri dispatcher
=head1 SYNOPSIS
package Your::Dispatcher;
use HTTPx::Dispatcher;
connect ':controller/:action/:id';
package Your::Handler;
use HTTP::Engine;
use Your::Dispatcher;
use UNIVERSAL::require;
my $dispatcher = HTTPx::Dispatcher->new;
$dispatcher->add_rule(':controller/:action/:id');
HTTP::Engine->new(
'config.yaml',
handle_request => sub {
my $c = shift;
my $rule = Your::Dispatcher->match($c->req);
my $rule = $dispatcher->match($c->req);
$rule->{controller}->use or die 'hoge';
my $action = $rule->{action};
$rule->{controller}->$action( $c->req );
Expand All @@ -89,7 +88,9 @@ lestrrat
=head1 SEE ALSO
L<HTTP::Engine>, L<Routes>
L<HTTP::Engine>,
L<http://api.rubyonrails.org/classes/ActionController/Routing.html>,
L<http://api.rubyonrails.org/classes/ActionController/Routing/RouteSet/Mapper.html>
=head1 LICENSE
Expand Down
85 changes: 85 additions & 0 deletions lib/HTTPx/Dispatcher/Declare.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package HTTPx::Dispatcher::Declare;
use strict;
use warnings;
use HTTPx::Dispatcher;
use Exporter 'import';

our @EXPORT = qw/connect match uri_for dispatcher/;

my $dispatcher = {};
sub dispatcher {
my $pkg = shift || caller(1);
return ( $dispatcher->{$pkg} ||= HTTPx::Dispatcher->new );
}

sub connect {
my @args = @_;
return dispatcher()->add_rule(@args);
}

sub match {
my ( $class, $req ) = @_;
return $class->dispatcher()->match($req);
}

sub uri_for {
my ( $class, @args ) = @_;
return $class->dispatcher()->uri_for(@args);
}

1;
__END__
=for stopwords TODO URI uri
=encoding utf8
=head1 NAME
HTTPx::Dispatcher::Declare - declarative dispatcher
=head1 SYNOPSIS
package Your::Dispatcher;
use HTTPx::Dispatcher::Declare;
connect ':controller/:action/:id';
package Your::Handler;
use HTTP::Engine;
use Your::Dispatcher;
use UNIVERSAL::require;
HTTP::Engine->new(
'config.yaml',
handle_request => sub {
my $c = shift;
my $rule = Your::Dispatcher->match($c->req);
$rule->{controller}->use or die 'hoge';
my $action = $rule->{action};
$rule->{controller}->$action( $c->req );
}
);
=head1 DESCRIPTION
HTTPx::Dispatcher::Declare is DSL for L<HTTPx::Dispatcher>.
=head1 AUTHOR
Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt>
=head1 THANKS TO
lestrrat
=head1 SEE ALSO
L<HTTPx::Dispatcher>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
8 changes: 6 additions & 2 deletions t/00_compile.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use strict;
use Test::More tests => 1;
use Test::More tests => 3;

BEGIN { use_ok 'HTTPx::Dispatcher' }
BEGIN {
use_ok 'HTTPx::Dispatcher';
use_ok 'HTTPx::Dispatcher::Rule';
use_ok 'HTTPx::Dispatcher::Declare';
}
4 changes: 2 additions & 2 deletions t/01_simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings;
use Test::Base;
use YAML;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
use HTTP::Request;
use Test::MockObject;

Expand Down Expand Up @@ -44,7 +44,7 @@ sub _proc {
my $pkg = "t::Dispatcher::" . ++$cnt;
eval <<"...";
package $pkg;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
$input;
...

Expand Down
5 changes: 2 additions & 3 deletions t/02_uri_for.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use strict;
use warnings;
use Test::Base;
use YAML;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Rule;
use HTTPx::Dispatcher::Declare;
use HTTP::Request;

plan tests => 1*blocks;
Expand All @@ -27,7 +26,7 @@ sub _eval {

eval <<"...";
package $pkg;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
$input;
...

Expand Down
4 changes: 2 additions & 2 deletions t/03_templates.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings;
use Test::Base;
use YAML;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
use HTTP::Request;
use Test::MockObject;

Expand Down Expand Up @@ -44,7 +44,7 @@ sub _proc {
my $pkg = "t::Dispatcher::" . ++$cnt;
eval <<"...";
package $pkg;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
$input;
...

Expand Down
5 changes: 2 additions & 3 deletions t/04_uri_for_templates.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use strict;
use warnings;
use Test::Base;
use YAML;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Rule;
use HTTPx::Dispatcher::Declare;
use HTTP::Request;

plan tests => 1*blocks;
Expand All @@ -27,7 +26,7 @@ sub _eval {

eval <<"...";
package $pkg;
use HTTPx::Dispatcher;
use HTTPx::Dispatcher::Declare;
$input;
...

Expand Down

0 comments on commit 3e38538

Please sign in to comment.