[Bps-public-commit] r14608 - in Path-Dispatcher/trunk: lib/Path lib/Path/Dispatcher t

sartak at bestpractical.com sartak at bestpractical.com
Tue Jul 29 13:36:56 EDT 2008


Author: sartak
Date: Tue Jul 29 13:36:55 2008
New Revision: 14608

Added:
   Path-Dispatcher/trunk/lib/Path/Dispatcher/
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm
Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
   Path-Dispatcher/trunk/t/001-api.t

Log:
 r67888 at onn:  sartak | 2008-07-29 13:36:52 -0400
 Reify rules


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	Tue Jul 29 13:36:55 2008
@@ -3,16 +3,34 @@
 use Moose;
 use MooseX::AttributeHelpers;
 
+use Path::Dispatcher::Rule;
+
+sub rule_class { 'Path::Dispatcher::Rule' }
+
 has rules => (
     metaclass => 'Collection::Array',
     is        => 'rw',
-    isa       => 'ArrayRef',
+    isa       => 'ArrayRef[Path::Dispatcher::Rule]',
     default   => sub { [] },
     provides  => {
-        push => 'add_rule',
+        push => '_add_rule',
     },
 );
 
+sub add_rule {
+    my $self = shift;
+
+    my $rule;
+    if (@_ == 1 && blessed($_[0])) {
+        $rule = shift;
+    }
+    else {
+        $rule = $self->rule_class->new(@_);
+    }
+
+    $self->_add_rule($rule);
+}
+
 sub dispatch {
     my $self = shift;
 

Added: Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule.pm	Tue Jul 29 13:36:55 2008
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+package Path::Dispatcher::Rule;
+use Moose;
+
+has stage => (
+    is       => 'rw',
+    isa      => 'Str',
+    default  => 'on',
+    required => 1,
+);
+
+has match => (
+    is       => 'rw',
+    isa      => 'Regexp',
+    required => 1,
+);
+
+has run => (
+    is       => 'rw',
+    isa      => 'CodeRef',
+    required => 1,
+);
+
+around BUILDARGS => sub {
+    my $orig = shift;
+    my $self = shift;
+    my $args = $self->$orig(@_);
+
+    $args->{match} = qr/$args->{match}/
+        if !ref($args->{match});
+
+    return $args;
+};
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+

Modified: Path-Dispatcher/trunk/t/001-api.t
==============================================================================
--- Path-Dispatcher/trunk/t/001-api.t	(original)
+++ Path-Dispatcher/trunk/t/001-api.t	Tue Jul 29 13:36:55 2008
@@ -9,7 +9,7 @@
 my $dispatcher = Path::Dispatcher->new;
 $dispatcher->add_rule(
     stage => 'on',
-    match => '*',
+    match => 'foo',
     run   => sub { ++$calls },
 );
 



More information about the Bps-public-commit mailing list