File Coverage

lib/Egg/Dispatch/Fast.pm
Criterion Covered Total %
statement 15 47 31.9
branch 0 6 0.0
condition 0 12 0.0
subroutine 5 13 38.4
pod 1 1 100.0
total 21 79 26.5


line stmt bran cond sub pod time code
1             package Egg::Dispatch::Fast;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Fast.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   822 use strict;
  1         9  
  1         53  
8 1     1   7 use warnings;
  1         3  
  1         49  
9 1     1   7 use base qw/ Egg::Dispatch /;
  1         5  
  1         293  
10              
11             our $VERSION= '3.02';
12              
13             sub dispatch {
14 0   0 0 1   $_[0]->{Dispatch} ||= Egg::Dispatch::Fast::handler->new(@_);
15             }
16              
17             package Egg::Dispatch::Fast::handler;
18 1     1   7 use strict;
  1         2  
  1         47  
19 1     1   5 use base qw/Egg::Dispatch::handler/;
  1         4  
  1         1417  
20              
21             __PACKAGE__->mk_accessors(qw/ mode_now _action_code /);
22              
23             sub _initialize {
24 0     0     my($self)= @_;
25 0   0       $self->mode( $self->e->_get_mode || $self->e->snip->[0] || "" );
26 0           $self->SUPER::_initialize;
27             }
28             sub _start {
29 0     0     my($self)= @_;
30 0           my $map= $self->e->dispatch_map;
31 0           my($code, $mode, $label);
32 0 0         if ($code= $map->{$self->mode}) {
    0          
33 0           $mode= $self->mode_now($self->mode);
34 0           $self->action([$mode]);
35             } elsif ($code= $map->{$self->default_mode}) {
36 0           $mode= $self->mode_now($self->default_mode);
37 0           $self->action([$self->default_name]);
38             } else {
39 0     0     $code= sub {};
  0            
40 0           $mode= $self->mode_now($self->default_mode);
41 0           $self->action([$self->default_name]);
42             }
43 0 0         if (ref($code) eq 'HASH') {
44 0   0       $self->label([ $code->{label} || $self->action->[0] ]);
45 0           $self->page_title( $self->label->[0] );
46 0   0 0     $self->_action_code( $code->{action} || sub {} );
  0            
47             } else {
48 0           $self->label([ $self->action->[0] ]);
49 0           $self->page_title( $self->label->[0] );
50 0           $self->_action_code( $code );
51             }
52             }
53             sub _action {
54 0     0     my($self)= @_;
55 0   0       my $action= $self->_action_code
56             || return $self->e->finished('404 Not Found');
57 0           $action->($self->e, $self);
58 0           1;
59             }
60 0     0     sub _finish { 1 }
61              
62             sub _example_code {
63 0     0     my($self)= @_;
64 0           my $a= { project_name=> $self->e->namespace };
65              
66 0           <<END_OF_EXAMPLE;
67             #
68             # Example of controller and dispatch.
69             #
70             package $a->{project_name}::Dispatch;
71             use strict;
72             use warnings;
73             use $a->{project_name}::Members;
74             use $a->{project_name}::BBS;
75              
76             our $VERSION= '0.01';
77              
78             $a->{project_name}-&gt;dispatch_map(
79              
80             # HASH can be used for the value.
81             # Please define not HASH but CODE if you do not use the label.
82             _default => {
83             label => 'index page.',
84             action => sub {},
85             },
86              
87             # If it is a setting only of the label, 'action' is omissible.
88             # Empty CODE tries to be set in action when omitting it, and to use
89             # 'help.tt' for the template.
90             help => { label => 'help page.' },
91              
92             members => &yen;&$a->{project_name}::Members::default,
93             members_login => &yen;&$a->{project_name}::Members::login,
94             members_logout => &yen;&$a->{project_name}::Members::logout,
95              
96             bbs => &yen;&$a->{project_name}::BBS::article_view,
97             bbs_post => &yen;&$a->{project_name}::BBS::article_post,
98             bbs_edit => &yen;&$a->{project_name}::BBS::article_edit,
99              
100             );
101              
102             1;
103             END_OF_EXAMPLE
104             }
105              
106             1;
107              
108             __END__
109              
110             =head1 NAME
111              
112             Egg::Dispatch::Fast - Another dispatch class.
113              
114             =head1 SYNOPSIS
115              
116             package MyApp::Dispatch;
117             use base qw/ Egg::Dispatch::Fast /;
118            
119             Egg->dispatch_map(
120            
121             _default => {
122             label=> 'index page.',
123             action => sub { ... },
124             },
125            
126             # When only the label is set, an empty CODE reference is set to action.
127             # And, hooo.tt was set in the template.
128             hooo => { label => 'hooo page.' },
129            
130             hoge => {
131             label => 'hoge page',
132             action => sub { ... },
133             },
134            
135             boo => sub { ... },
136            
137             );
138              
139             =head1 DESCRIPTION
140              
141             L<EggDispatch::Standard> it is a plugin to do more high-speed Dispatch.
142              
143             As for 'dispatch_map', only a single hierarchy is treatable.
144              
145             The regular expression etc. cannot be used for the key.
146              
147             The value to the key should be CODE reference.
148              
149             The argument passed for the CODE reference is L<Egg::Dispatch::Standard>.
150             It is similar.
151              
152             =head1 METHODS
153              
154             L<Egg::Dispatch> has been succeeded to.
155              
156             =head2 dispatch
157              
158             The Egg::Dispatch::Fast::handler object is returned.
159              
160             my $d= $e->dispatch;
161              
162             =head1 HANDLER METHODS
163              
164             =head2 mode_now
165              
166             The action matched with 'dispatch_map' is returned as a mode.
167              
168             * The value of 'default_mode' method is returned when failing in the match.
169              
170             =head1 SEE ALSO
171              
172             L<Egg::Release>,
173             L<Egg::Dispatch>,
174              
175             =head1 AUTHOR
176              
177             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
178              
179             =head1 COPYRIGHT AND LICENSE
180              
181             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
182              
183             This library is free software; you can redistribute it and/or modify
184             it under the same terms as Perl itself, either Perl version 5.8.6 or,
185             at your option, any later version of Perl 5 you may have available.
186              
187             =cut
188