[Bps-public-commit] Path-Dispatcher branch, master, updated. 6134cc3952297c877375ea5cb7084c744050b6e8

sartak at bestpractical.com sartak at bestpractical.com
Thu Jan 7 16:51:02 EST 2010


The branch, master has been updated
       via  6134cc3952297c877375ea5cb7084c744050b6e8 (commit)
      from  26e5df863e1ecbfed8de65423ac4e3f23693a004 (commit)

Summary of changes:
 lib/Path/Dispatcher/Rule.pm                 |    1 +
 lib/Path/Dispatcher/Rule/{Eq.pm => Enum.pm} |   42 ++++++++++++++++++---------
 t/025-sequence-custom-rule.t                |   33 +++------------------
 3 files changed, 34 insertions(+), 42 deletions(-)
 copy lib/Path/Dispatcher/Rule/{Eq.pm => Enum.pm} (51%)

- Log -----------------------------------------------------------------
commit 6134cc3952297c877375ea5cb7084c744050b6e8
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Thu Jan 7 16:50:54 2010 -0500

    Add enum rule for convenience

diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 90ad4be..5e384cb 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -145,6 +145,7 @@ require Path::Dispatcher::Rule::Chain;
 require Path::Dispatcher::Rule::CodeRef;
 require Path::Dispatcher::Rule::Dispatch;
 require Path::Dispatcher::Rule::Empty;
+require Path::Dispatcher::Rule::Enum;
 require Path::Dispatcher::Rule::Eq;
 require Path::Dispatcher::Rule::Intersection;
 require Path::Dispatcher::Rule::Metadata;
diff --git a/lib/Path/Dispatcher/Rule/Enum.pm b/lib/Path/Dispatcher/Rule/Enum.pm
new file mode 100644
index 0000000..5d483ac
--- /dev/null
+++ b/lib/Path/Dispatcher/Rule/Enum.pm
@@ -0,0 +1,104 @@
+package Path::Dispatcher::Rule::Enum;
+use Any::Moose;
+extends 'Path::Dispatcher::Rule';
+
+has enum => (
+    is       => 'rw',
+    isa      => 'ArrayRef[Str]',
+    required => 1,
+);
+
+has case_sensitive => (
+    is      => 'rw',
+    isa     => 'Bool',
+    default => 1,
+);
+
+sub _match {
+    my $self = shift;
+    my $path = shift;
+
+    if ($self->case_sensitive) {
+        for my $value (@{ $self->enum }) {
+            return 1 if $path->path eq $value;
+        }
+    }
+    else {
+        for my $value (@{ $self->enum }) {
+            return 1 if lc($path->path) eq lc($value);
+        }
+    }
+}
+
+sub _prefix_match {
+    my $self = shift;
+    my $path = shift;
+
+    my $truncated = substr($path->path, 0, length($self->string));
+
+    if ($self->case_sensitive) {
+        for my $value (@{ $self->enum }) {
+            return (1, substr($path->path, length($self->string)))
+                if $truncated eq $value;
+        }
+    }
+    else {
+        for my $value (@{ $self->enum }) {
+            return (1, substr($path->path, length($self->string)))
+                if lc($truncated) eq lc($value);
+        }
+    }
+}
+
+sub complete {
+    my $self = shift;
+    my $path = shift->path;
+    my @completions;
+
+    if ($self->case_sensitive) {
+        for my $value (@{ $self->enum }) {
+            my $partial = substr($value, 0, length($path));
+            push @completions, $value if $partial eq $path;
+        }
+    }
+    else {
+        for my $value (@{ $self->enum }) {
+            my $partial = substr($value, 0, length($path));
+            push @completions, $value if lc($partial) eq lc($path);
+        }
+    }
+
+    return @completions;
+}
+
+sub readable_attributes { q{"} . shift->string . q{"} }
+
+__PACKAGE__->meta->make_immutable;
+no Any::Moose;
+
+1;
+
+__END__
+
+=head1 NAME
+
+Path::Dispatcher::Rule::Eq - predicate is a string equality
+
+=head1 SYNOPSIS
+
+    my $rule = Path::Dispatcher::Rule::Eq->new(
+        string => 'comment',
+        block  => sub { display_comment($2) },
+    );
+
+=head1 DESCRIPTION
+
+Rules of this class simply check whether the string is equal to the path.
+
+=head1 ATTRIBUTES
+
+=head2 string
+
+=cut
+
+
diff --git a/t/025-sequence-custom-rule.t b/t/025-sequence-custom-rule.t
index 460d193..20a1ca7 100644
--- a/t/025-sequence-custom-rule.t
+++ b/t/025-sequence-custom-rule.t
@@ -8,35 +8,12 @@ my @calls;
 
 do {
     package MyApp::Dispatcher::Rule::Language;
-    use Moose;
-    extends 'Path::Dispatcher::Rule';
+    use Any::Moose;
+    extends 'Path::Dispatcher::Rule::Enum';
 
-    my @langs = qw/ruby perl php python/;
-
-    sub _match {
-        my $self = shift;
-        my $path = shift;
-
-        for my $lang (@langs) {
-            return $lang if $path->path eq $lang;
-        }
-
-        return;
-    }
-
-    sub complete {
-        my $self = shift;
-        my $path = shift->path;
-
-        my @completions;
-
-        for my $lang (@langs) {
-            my $partial = substr($lang, 0, length($path));
-            push @completions, $lang if $partial eq $path;
-        }
-
-        return @completions;
-    }
+    has '+enum' => (
+        default => sub { [qw/ruby perl php python/] },
+    );
 };
 
 my $dispatcher = Path::Dispatcher->new(

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list