Skip to content

Commit

Permalink
no Mo*se
Browse files Browse the repository at this point in the history
  • Loading branch information
masaki committed Feb 28, 2010
1 parent 6b0d998 commit a2e31a8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 125 deletions.
92 changes: 47 additions & 45 deletions lib/HTTP/Router.pm
@@ -1,42 +1,44 @@
package HTTP::Router;

use 5.008_001;
use Any::Moose;
use Any::Moose 'X::AttributeHelpers';
use strict;
use warnings;
use base 'Class::Accessor::Fast';
use Hash::AsObject;
use List::MoreUtils 'part';
use HTTP::Router::Route;

our $VERSION = '0.03';

has 'routes' => (
is => 'ro',
isa => 'ArrayRef',
metaclass => 'Collection::Array',
lazy => 1,
builder => '_build_routes',
auto_deref => 1,
provides => {
push => 'add_route',
clear => 'clear_routes',
},
);

has 'use_inline_match' => (
is => 'rw',
isa => 'Bool',
default => 1,
);

has 'inline_matcher' => (
is => 'rw',
isa => 'CodeRef',
lazy_build => 1,
);

sub _build_routes { [] }

sub _build_inline_matcher {
__PACKAGE__->mk_accessors(qw'code');

sub new {
my $class = shift;
return bless { routes => [], code => undef }, $class;
}

sub routes {
my $self = shift;
@{ $self->{routes} };
}

sub add_route {
my ($self, $route, @args) = @_;

unless (ref $route) {
$route = HTTP::Router::Route->new(path => $route, @args);
}

$self->thaw;
push @{ $self->{routes} }, $route;
}

sub clear_routes {
my $self = shift;
$self->{routes} = [];
}

sub _build_code {
my $self = shift;

my ($path_routes, $capture_routes) =
Expand All @@ -62,30 +64,31 @@ sub _build_inline_matcher {
};
}

around 'add_route' => sub {
my ($next, $self, $route, @args) = @_;

unless (blessed $route) {
$route = HTTP::Router::Route->new(path => $route, @args);
}
sub freeze {
my $self = shift;
$self->code( $self->_build_code );
$self;
}

$self->clear_inline_matcher if $self->has_inline_matcher;
$next->($self, $route);
};
sub thaw {
my $self = shift;
$self->code(undef);
$self;
}

sub reset {
my $self = shift;
$self->thaw;
$self->clear_routes;
$self->clear_inline_matcher if $self->has_inline_matcher;
$self;
}

sub match {
my $self = shift;
my $req = blessed $_[0] ? $_[0] : Hash::AsObject->new(path => $_[0], %{ $_[1] || {} });
my $req = ref $_[0] ? $_[0] : Hash::AsObject->new(path => $_[0], %{ $_[1] || {} });

if ($self->use_inline_match) {
return $self->inline_matcher->($req);
if ($self->code) {
return $self->code->($req);
}
else {
for my $route ($self->routes) {
Expand All @@ -107,8 +110,7 @@ sub route_for {
return;
}

no Any::Moose;
__PACKAGE__->meta->make_immutable;
1;

=head1 NAME
Expand Down
18 changes: 11 additions & 7 deletions lib/HTTP/Router/Debug.pm
@@ -1,10 +1,18 @@
package HTTP::Router::Debug;

use Any::Moose '::Role';
use strict;
use warnings;
use Text::SimpleTable;
use HTTP::Router;

requires 'routes';
our @EXPORT = qw(show_table routing_table);

sub import {
require HTTP::Router;
no strict 'refs';
for my $name (@EXPORT) {
*{"HTTP::Router::$name"} = \&{$name};
}
}

sub show_table {
my $table = $_[0]->routing_table->draw;
Expand Down Expand Up @@ -36,10 +44,6 @@ sub routing_table {
return $table;
}

# apply self to HTTP::Router
__PACKAGE__->meta->apply(HTTP::Router->meta);

no Any::Moose '::Role';
1;

=head1 NAME
Expand Down
46 changes: 21 additions & 25 deletions lib/HTTP/Router/Match.pm
@@ -1,30 +1,26 @@
package HTTP::Router::Match;

use Any::Moose;

has 'params' => (
is => 'ro',
isa => 'HashRef',
lazy => 1,
default => sub { +{} },
);

has 'captures' => (
is => 'ro',
isa => 'HashRef',
lazy => 1,
default => sub { +{} },
);

has 'route' => (
is => 'ro',
isa => 'HTTP::Router::Route',
handles => ['uri_for'],
required => 1,
);

no Any::Moose;
__PACKAGE__->meta->make_immutable;
use strict;
use warnings;
use base 'Class::Accessor::Fast';

__PACKAGE__->mk_accessors(qw'params captures route');

sub new {
my ($class, %args) = @_;
return bless {
params => {},
captures => {},
%args,
}, $class;
}

sub uri_for {
my ($self, @args) = @_;
$self->route->uri_for(@args);
}

1;

=head1 NAME
Expand Down
101 changes: 53 additions & 48 deletions lib/HTTP/Router/Route.pm
@@ -1,53 +1,59 @@
package HTTP::Router::Route;

use Any::Moose;
use Any::Moose 'X::AttributeHelpers';
use URI::Template::Restrict 0.03;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
use URI::Template::Restrict;
use HTTP::Router::Match;

has 'path' => (
is => 'rw',
isa => 'Str',
metaclass => 'String',
lazy_build => 1,
provides => { append => 'append_path' },
);

has 'params' => (
is => 'rw',
isa => 'HashRef',
metaclass => 'Collection::Hash',
lazy_build => 1,
provides => { set => 'add_params' },
);

has 'conditions' => (
is => 'rw',
isa => 'HashRef',
metaclass => 'Collection::Hash',
lazy_build => 1,
provides => { set => 'add_conditions' },
);

has 'parts' => (
is => 'rw',
isa => 'Int',
lazy => 1,
default => sub { $_[0]->path =~ tr!/!/! },
);

has 'templates' => (
is => 'rw',
isa => 'URI::Template::Restrict',
lazy_build => 1,
handles => [qw'variables extract'],
);

sub _build_path { '' }
sub _build_params { {} }
sub _build_conditions { {} }

sub _build_templates { URI::Template::Restrict->new($_[0]->path) }
__PACKAGE__->mk_accessors(qw'path params conditions');

sub new {
my ($class, %args) = @_;
return bless {
path => '',
params => {},
conditions => {},
%args,
}, $class;
}

sub parts {
my $self = shift;
$self->{parts} ||= $self->path =~ tr!/!/!;
}

sub append_path {
my ($self, $path) = @_;
$self->{path} .= (defined $path ? $path : '');
}

{
no strict 'refs';
for my $name (qw'params conditions') {
*{"add_${name}"} = sub {
my ($self, %args) = @_;
while (my ($key, $value) = each %args) {
$self->$name->{$key} = $value;
}
};
}
}

sub templates {
my $self = shift;
$self->{templates} ||= URI::Template::Restrict->new($self->path);
}

{
no strict 'refs';
for my $method (qw'variables extract') {
*{$method} = sub {
my ($self, @args) = @_;
$self->templates->$method(@args);
};
}
}

sub match {
my ($self, $req) = @_;
Expand Down Expand Up @@ -137,8 +143,7 @@ sub uri_for {
return $self->templates->process_to_string(%$args);
}

no Any::Moose;
__PACKAGE__->meta->make_immutable;
1;

=for stopwords params
Expand Down

0 comments on commit a2e31a8

Please sign in to comment.