[Jifty-commit] r442 - jifty/trunk/lib jifty/trunk/lib/Jifty jifty/trunk/t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 2 13:15:47 EST 2006


Author: jesse
Date: Mon Jan  2 13:15:47 2006
New Revision: 442

Added:
   jifty/trunk/t/04-dispatcher.t
Modified:
   /   (props changed)
   jifty/trunk/   (props changed)
   jifty/trunk/META.yml
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty.pm
   jifty/trunk/lib/Jifty/Dispatcher.pm
   jifty/trunk/lib/Jifty/Everything.pm

Log:
 r22050 at truegrounds:  jesse | 2006-01-02 19:10:21 +0100
 * A bit more fleshing out of the Dispatcher


Modified: jifty/trunk/META.yml
==============================================================================
--- jifty/trunk/META.yml	(original)
+++ jifty/trunk/META.yml	Mon Jan  2 13:15:47 2006
@@ -59,6 +59,7 @@
   Time::HiRes: 0
   Time::ParseDate: 0
   UNIVERSAL::require: 0
+  UNIVERSAL: 0
   URI: 0
   WWW::Mechanize: 1.12
   XML::Writer: 0

Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Mon Jan  2 13:15:47 2006
@@ -1,7 +1,8 @@
 use inc::Module::Install 0.46;
 name('Jifty');
+
 # this is a dev version, CPAN.pm will not install by default
-requires(perl => '5.8.3');
+requires(perl => '5.8.3'); 
 requires('Apache::Session');
 requires('App::CLI' => 0.03 ); # App::CLI::Command::Help App::CLI::Command
 requires('CGI');                   # CGI::Fast
@@ -56,6 +57,7 @@
 requires('Time::HiRes');
 requires('Time::ParseDate');
 requires('UNIVERSAL::require');
+requires('UNIVERSAL');
 requires('URI');
 requires('WWW::Mechanize' => 1.12);
 requires('XML::Writer');

Modified: jifty/trunk/lib/Jifty.pm
==============================================================================
--- jifty/trunk/lib/Jifty.pm	(original)
+++ jifty/trunk/lib/Jifty.pm	Mon Jan  2 13:15:47 2006
@@ -61,7 +61,7 @@
 
 use base qw/Jifty::Object/;
 
-use vars qw/$HANDLE $CONFIG $LOGGER/;
+use vars qw/$HANDLE $CONFIG $LOGGER $DISPATCHER/;
 
 =head1 METHODS
 
@@ -103,17 +103,16 @@
 
     # Load the configuration. stash it in ->config
     __PACKAGE__->config( Jifty::Config->new() );
+
     __PACKAGE__->logger( Jifty::Logger->new( $args{'logger_component'} ) );
+    __PACKAGE__->dispatcher(Jifty::Dispatcher->new());
 
-    my $loader = Jifty::ClassLoader->new();
-    $loader->require;
 
-    unless ( $args{'no_handle'} or not Jifty->config->framework('Database') )
-    {
-        Jifty->handle( Jifty::Handle->new() );
-        Jifty->handle->connect();
-        Jifty->handle->check_schema_version();
-    }
+   # Get a classloader set up
+   Jifty::ClassLoader->new->require;
+
+   # Let's get the database rocking and rolling
+   __PACKAGE__->setup_database_connection(%args);
 
 }
 
@@ -155,9 +154,23 @@
     return $HANDLE;
 }
 
+=head2 dispatcher
+
+An accessor for the C<Jifty::Dispatcher> object that we use to make decisions about how
+to dispatch each request made by a web client.
+
+
+=cut
+
+sub dispatcher {
+    my $class = shift;
+    $DISPATCHER = shift if (@_);
+    return $DISPATCHER;
+}
+
 =head2 web
 
-An accessor for the L<Jifty::Web> object that the web interface uses.
+An accessor for the L<Jifty::Web> object that the web interface uses. 
 
 =cut
 
@@ -167,6 +180,38 @@
 }
 
 
+=head2 setup_database_connection
+
+Set up our database connection. Optionally takes a param hash with a single argument
+
+=over
+
+=item no_handle
+
+Defaults to false. If true, Jifty won't try to set up a database handle
+
+=back
+
+
+If C<no_handle> is set or our application's config file is missing a C<Database> configuration
+ section or I<has> a C<SkipDatabase: 1> directive in its framework configuration, does nothing.
+
+=cut
+
+sub setup_database_connection {
+    my $self = shift;
+    my %args = (no_handle =>0,
+                @_);
+    unless ( $args{'no_handle'}
+        or __PACKAGE__->config->framework('SkipDatabase')
+        or not __PACKAGE__->config->framework('Database') )
+    {
+        __PACKAGE__->handle( Jifty::Handle->new() );
+        __PACKAGE__->handle->connect();
+        __PACKAGE__->handle->check_schema_version();
+    }
+}
+
 =head1 LICENSE
 
 Jifty is Copyright 2005 Best Practical Solutions, LLC.

Modified: jifty/trunk/lib/Jifty/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Dispatcher.pm	Mon Jan  2 13:15:47 2006
@@ -1,3 +1,11 @@
+package Jifty::Dispatcher;
+use warnings;
+use strict;
+use Jifty;
+use UNIVERSAL qw(isa);
+
+use vars qw/%_TABLE @ENT/;
+
 =head1 NAME
 
 Jifty::Dispatcher
@@ -39,10 +47,10 @@
 package MyWeblog::Dispatcher;
 use base 'Jifty::Dispatcher';
 
-url qr|^/error/| => { render_page };
-url qr|^/| => { Jifty::Web->handle_request }; # XXX TODO, DO WE WANT THIS HERE OR AT THE END?
-url qr|/_elements/| => { redirect( url => '/errors/'.$dispatcher->url)  };             
-url qr|^/let/(.*)$| => {
+on url qr|^/error/|, run { render_page };
+on url qr|^/|, run { Jifty::Web->handle_request }; # XXX TODO, DO WE WANT THIS HERE OR AT THE END?
+on url qr|/_elements/|, run { redirect( url => '/errors/'.$dispatcher->url)  };             
+on url qr|^/let/(.*)$|, run {
 
     # Because we're granting permissions on /let/... based on an auth token
     # we tighten up the ::Action::.* permissions. 
@@ -101,4 +109,100 @@
 
 =cut 
 
+=head1 IMPLEMENTATION
+
+=head2 new 
+
+Create a new dispatcher object
+
+=cut
+
+sub new {
+    my $class = shift;
+    my $self  = {};
+    bless $self, $class;
+    return $self;
+}
+
+=for private on
+
+C<on> takes named arguments. 
+
+=over
+
+=item condition
+
+A coderef that runs to determine if this rule cares about the current request 
+
+=item action
+
+This rgument is a coderef that Jifty should run when the "condition" 
+coderef returns true.
+
+=item priority 
+
+This argument is an integer priority that determines what order the rules
+will run in.  Priority C<1> rules run first, followed by priority C<2>
+rules. Order within a priority isn't guaranteed.  We recommend you use
+priorities between C<100> and C<200> for every day activities.
+
+In the future, Jifty should autoincrement rule priorities.
+
+=back
+
+=cut
+
+sub on {
+    my $self = Jifty->dispatcher;
+    
+    my %args = (
+        condition => sub {undef},
+        action    => sub {undef},
+        priority  => undef,
+        @_
+    );
+    $self->add_entry(
+        priority => $args{'priority'},
+        entry    => \%args
+    );
+
+    return (1);
+}
+
+=head2 add_entry
+
+instance method
+
+=cut
+
+sub add_entry {
+    my $self = shift;
+    my %args = (
+        priority => undef,
+        entry    => undef,
+        @_
+    );
+
+    $args{'priority'} ||= 100;
+    warn "Can't add a dispatch table entry without content"
+        unless ( $args{'entry'} );
+    push @{ $self->{'_entries'}{$args{'priority'} }}, $args{'entry'};
+}
+
+sub entries {
+    my $self = shift;
+    return map @{ $self->{'_entries'}{$_} }, sort keys %{$self->{'_entries'}};
+}
+
+sub url ($) {
+    my $url = shift;
+
+    return ( condition => sub { die "need to implement matcher for $url"; } );
+}
+
+sub run (&) {
+    my $action = shift;
+    return ( action => $action );
+}
+
 1;

Modified: jifty/trunk/lib/Jifty/Everything.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Everything.pm	(original)
+++ jifty/trunk/lib/Jifty/Everything.pm	Mon Jan  2 13:15:47 2006
@@ -12,6 +12,7 @@
 # Could use Module::Pluggable, I guess.
 
 use Jifty;
+use Jifty::Dispatcher;
 use Jifty::Object;
 use Jifty::Config;
 use Jifty::Handle;

Added: jifty/trunk/t/04-dispatcher.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/04-dispatcher.t	Mon Jan  2 13:15:47 2006
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Test::More qw/no_plan/;
+use_ok('Jifty::Dispatcher');
+use_ok('Jifty');
+ok(Jifty->new(no_handle => 1));
+my $d = Jifty::Dispatcher->new();
+
+can_ok($d,'on');
+
+
+ok(Jifty::Dispatcher::on( condition => sub { 1 }, action => sub {2}, priority => 25));
+my @entries = Jifty->dispatcher->entries();
+is (scalar @entries, 1);
+is (&{$entries[0]->{condition}},1);
+is (&{$entries[0]->{action}},2);
+
+ok(Jifty::Dispatcher::on( condition => sub { 1 }, action => sub {2}, priority => 25));
+ at entries = Jifty->dispatcher->entries();
+is (scalar @entries, 2);
+
+eval 'package Jifty::Dispatcher;  on url "foo", run { qq{xxx} }; ';
+
+ok(!$@, $@);
+ at entries = Jifty->dispatcher->entries();
+
+is (scalar @entries, 3);
+
+
+


More information about the Jifty-commit mailing list