Genericish dispatcher/controller logic with Jifty

jesse on 2006-11-29T19:02:51

In the past couple days, a few folks from the CGI::App and Catalyst communities have posted examples of their dispatcher/controller layout and syntax. Here's what a similar example might look like in Jifty:

use warnings;
use strict;

package MyApp::Dispatcher; use Jifty::Dispatcher -base;

under '/admin/account' => run { before '*' => set object_type => 'account'; before qr'/(\d+)' => run { set id => $1; };

on '/' => show 'crud/index'; on '/create' => show 'crud/create'; on '/*/update' => show 'crud/update'; on '/*' => show 'crud/view'; };



Edit: jibsheet pointed out that I'd misunderstood the Catalyst example. the code above should now be a more faithful implementation


Redundant run{...}

audreyt on 2006-11-29T19:41:20

Static rules such as:
before '*' => run { set object_type => 'account' };

on '/' => run { show 'crud/index' };
Can be written instead as:
before '*' => set object_type => 'account';

on '/' => show 'crud/index';
it's a bit more declarative that way. :-)

Redundant qr{...}

audreyt on 2006-11-29T19:51:11

Also, with our extended glob syntax one can replace:
before qr'/(.*?)/(.*)' => run { set id => $2; };
with this:
before '**/*' => run { set id => $2; };
With both changes in place, it'd look like this:
under '/admin/account' => run {
    before '*'    => set object_type => 'account';
    before '**/*' => run { set id => $2 };

    on '/'         => show 'crud/index';
    on '/list'     => show 'crud/list';
    on '/create'   => show 'crud/list';
    on '/view/*'   => show 'crud/view';
    on '/update/*' => show 'crud/update;
};

Re:Redundant qr{...}

jesse on 2006-11-29T19:56:47

Sorry. I changed some things out from under you. oops.

Small suggestion

sri on 2006-11-30T05:22:20

Me likes, but you might want to extend your globs a bit.
Range and hash support could make it look even better.



before qr'/(\d+)' => run { set id => $1; };

could become
# hash, equivalent to /(\d+)
before '/#' => run { set id => $1 }



# range, for more specific matching (bit more complicated to implement but worth it imo)
before '/[0-255]' => run { set id => $1 }



# alternatives, would make sense too :)
before '/{1|133|255}' => run { set id => $1 }

Re:Small suggestion

jesse on 2006-11-30T05:29:37

It's an interesting thought. Though once we get to ranges, it's almost easier to just use full regexps. (And everywhere we use globs, we can just use a full regexp and qr'' instead.

Globs are really nice for the simple case. I'm not quite sure where to draw the line. But "#" feels really nice. The suggestion is much appreciated.

Re:Small suggestion

audreyt on 2006-11-30T05:35:49

We already support the standard glob syntax:
before '{1,133,255}'
As well as character classes:
before '[a-z]'
So I'm afraid that numeric range needs some other syntax. The '#' shorthand is very nice, though. :-)

Re:Small suggestion

sri on 2006-11-30T06:35:03

Ah, nice one, been some time since i last looked at Jifty::Dispatcher. :)

Re:Small suggestion

audreyt on 2006-12-01T18:15:26

----------------------------------------------------------------------
r25678 (orig r2238):  audreyt | 2006-12-01 12:43:40 +0800

* Jifty::Dispatcher - NUMBER SIGN (#) now captures one or more digit
  characters in the extended shellglob condition syntax.
  Suggested by: Sebastian Riedel
----------------------------------------------------------------------