Create a class to separate out the handling of options, instead of 
mixing the logic with the main DW::Routing class.

        Patch by exor674. 

diffs (295 lines):

diff -r ca80e1825d70 -r a1e659a973c4 cgi-bin/DW/Routing.pm
--- a/cgi-bin/DW/Routing.pm     Tue Jun 22 17:39:44 2010 +0800
+++ b/cgi-bin/DW/Routing.pm     Tue Jun 22 19:53:06 2010 +0800
@@ -22,6 +22,7 @@ use DW::Template;
 use DW::Template;
 use JSON;
 use DW::Request;
+use DW::Routing::CallInfo;
 
 our %string_choices;
 our %regex_choices = (
@@ -97,22 +98,24 @@ sub get_call_opts {
     $opts{format} = $format;
 
     # we construct this object as an easy way to get options later, it gives
-    # us accessors.  FIXME: this should be a separate class, not DW::Routing.
-    my $call_opts = bless( \%opts, $class );
+    # us accessors.
+    my $call_opts = DW::Routing::CallInfo->new( \%opts );
 
     # try the string options first as they're fast
-    my $hash = $opts{__hash} = $string_choices{$opts{role} . $uri};
-    return $call_opts if defined $hash;
-
+    my $hash = $string_choices{$call_opts->role . $uri};
+    if ( defined $hash ) {
+        $call_opts->init_call_opts( $hash );
+        return $call_opts;
+    }
+    
     # try the regex choices next
     # FIXME: this should be a dynamically sorting array so the most used items 
float to the top
     # for now it doesn't matter so much but eventually when everything is in 
the routing table
     # that will have to be done
     my @args;
-    foreach $hash ( @{ $regex_choices{$opts{role}} } ) {
+    foreach $hash ( @{ $regex_choices{$call_opts->role} } ) {
         if ( ( @args = $uri =~ $hash->{regex} ) ) {
-            $opts{__hash} = $hash;
-            $opts{subpatterns} = \...@args;
+            $call_opts->init_call_opts( $hash, \...@args );
             return $call_opts;
         }
     }
@@ -131,7 +134,7 @@ sub call_hash {
     my ( $class, $opts ) = @_;
     my $r = DW::Request->get;
 
-    my $hash = $opts->{__hash};
+    my $hash = $opts->call_opts;
     return undef unless $hash && $hash->{sub};
 
     $r->pnote(routing_opts => $opts);
@@ -147,18 +150,17 @@ sub _call_hash {
 sub _call_hash {
     my $r = DW::Request->get;
     my $opts = $r->pnote('routing_opts');
-    my $hash = $opts->{__hash};
 
-    $opts->{format} ||= $hash->{format};
+    $opts->prepare_for_call;
 
-    my $format = $opts->{format};
+    my $format = $opts->format;
     $r->content_type( $default_content_types->{$format} )
         if $default_content_types->{$format};
 
     # try to call the handler that actually does the content creation; it will
-    # return either a string (valid result), or a number (HTTP code), or undef
+    # return either a number (HTTP code), or undef
     # means there was an error of some sort
-    my $ret = eval { return $hash->{sub}->( $opts ) };
+    my $ret = eval { $opts->call };
     return $ret unless $@;
 
     # here down is simply error handling for whatever the handler sub above
@@ -323,58 +325,6 @@ sub register_regex {
     push @{$regex_choices{user}}, $hash if $hash->{user};
 }
 
-=head1 Controller API
-
-API to be used from the controllers.
-
-=head2 C<< $self->args >>
-
-Return the arguments passed to the register call.
-
-=cut
-
-sub args { return $_[0]->{__hash}->{args}; }
-
-=head2 C<< $self->format >>
-
-Return the format.
-
-=cut
-
-sub format { return $_[0]->{format}; }
-
-=head2 C<< $self->role >>
-
-Current mode: 'app' or 'user' or 'ssl'
-
-=cut
-
-sub role { return $_[0]->{role}; }
-
-=head2 C<< $self->ssl >>
-
-Is SSL request?
-
-=cut
-
-sub ssl { return $_[0]->{role} eq 'ssl' ? 1 : 0; }
-
-=head2 C<< $self->subpatterns >>
-
-Return the regex matches.
-
-=cut
-
-sub subpatterns { return $_[0]->{subpatterns}; }
-
-=head2 C<< $self->username >>
-
-Username
-
-=cut
-
-sub username { return $_[0]->{username}; }
-
 =head1 AUTHOR
 
 =over
diff -r ca80e1825d70 -r a1e659a973c4 cgi-bin/DW/Routing/CallInfo.pm
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Routing/CallInfo.pm    Tue Jun 22 19:53:06 2010 +0800
@@ -0,0 +1,155 @@
+#!/usr/bin/perl
+#
+# DW::Routing::CallInfo
+#
+# Module to provide accessors for routing call info hashes.
+#
+# Authors:
+#      Andrea Nall <[email protected]>
+#      Mark Smith <[email protected]>
+#
+# Copyright (c) 2009-2010 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself.  For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+package DW::Routing::CallInfo;
+use strict;
+
+=head1 NAME
+
+DW::Routing::CallInfo - Module to provide accessors for routing call info 
hashes.
+
+=head1 SYNOPSIS
+
+=head2 C<< $class->new( $hash ) >>
+
+=cut
+
+sub new {
+    my ( $class, $hash ) = @_;
+
+    return bless $hash, $class;
+}
+
+=head2 C<< $self->call_opts( $hash ) >>
+
+Retrieve the call opts hash.
+
+=cut
+
+sub call_opts {
+    return $_[0]->{__hash};
+}
+
+=head2 C<< $self->init_call_opts( $hash, $subpatterns ) >>
+
+Initalizes the call opts.
+
+=cut
+
+sub init_call_opts {
+    my ($self, $hash, $args) = @_;
+
+    $self->{__hash} = $hash;
+    $self->{subpatterns} = $args;
+}
+
+=head2 C<< $self->prepare_for_call >>
+
+Prepares this CallInfo for being called.
+
+=cut
+
+sub prepare_for_call {
+    my $hash = $_[0]->{__hash};
+
+    $_[0]->{format} ||= $hash->{format};
+}
+
+=head2 C<< $self->call >>
+
+Calls the sub.
+
+=cut
+
+sub call {
+    my ( $opts ) = @_;
+
+    my @args;
+    @args = @{$opts->subpatterns} if ( $opts->subpatterns );
+    $opts->{__hash}->{sub}->( $opts, @args );
+}
+
+=head1 Controller API
+
+API to be used from the controllers.
+
+=head2 C<< $self->args >>
+
+Return the arguments passed to the register call.
+
+=cut
+
+sub args { return $_[0]->{__hash}->{args}; }
+
+=head2 C<< $self->format >>
+
+Return the format.
+
+=cut
+
+sub format { return $_[0]->{format}; }
+
+=head2 C<< $self->role >>
+
+Current mode: 'app' or 'user' or 'ssl'
+
+=cut
+
+sub role { return $_[0]->{role}; }
+
+=head2 C<< $self->ssl >>
+
+Is SSL request?
+
+=cut
+
+sub ssl { return $_[0]->{role} eq 'ssl' ? 1 : 0; }
+
+=head2 C<< $self->subpatterns >>
+
+Return the regex matches.
+
+=cut
+
+sub subpatterns {
+    return $_[0]->{subpatterns};
+}
+
+=head2 C<< $self->username >>
+
+Username
+
+=cut
+
+sub username { return $_[0]->{username}; }
+
+=head1 AUTHOR
+
+=item Andrea Nall <[email protected]>
+
+=item Mark Smith <[email protected]>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (c) 2009-2010 by Dreamwidth Studios, LLC.
+
+This program is free software; you may redistribute it and/or modify it under
+the same terms as Perl itself. For a copy of the license, please reference
+'perldoc perlartistic' or 'perldoc perlgpl'.
+
+=cut
+
+1;
\ No newline at end of file
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to