| 1 | package Plack::Middleware::Dispatch::GP; | ||||||||||||||||||||||||||||
| 2 | 1 | $Plack::Middleware::Dispatch::GP::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | Plack::Middleware::Dispatch::GP - dispatcher middleware for general purposes handling | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | The module provides Plack environment to registered dispatchers | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.01 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use parent "Plack::Middleware"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Plack::Util::Accessor qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | dispatch | ||||||||||||||||||||||||||||
| 24 | 1 | /; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Plack::Request (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $foo = Foo->new() # Foo should provide dispatch method | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $app = sub { ... }; | ||||||||||||||||||||||||||||
| 36 | builder { | ||||||||||||||||||||||||||||
| 37 | enable "Dispatch", dispatch => [\&cb, $foo]; | ||||||||||||||||||||||||||||
| 38 | $app; | ||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # cb() | ||||||||||||||||||||||||||||
| 42 | sub cb { | ||||||||||||||||||||||||||||
| 43 | my($env) = @_; | ||||||||||||||||||||||||||||
| 44 | ... | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 new($p) | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | C<$p> a hash reference | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | C<$p->{dispatch} [CODE reference or an object provides dispatch method] | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | (@_ == 1 && ref $_[0] eq "HASH") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 62 | || die "$class->new expects a has reference parameter"; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my $disp = delete($_[0]->{dispatch}); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | ($disp) || die "$class requires dispatch parameter"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 68 | my @d | ||||||||||||||||||||||||||||
| 69 | = ref($disp) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | ? @{$disp} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | : ($disp); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | for (my $i = 0; $i <= $#d; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (Scalar::Util::blessed($d[$i])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | my $cb = $d[$i]; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $cb->can("dispatch") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | || die ref($cb) . " doesn't provide dispatch()"; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $d[$i] = sub { $cb->dispatch(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 81 | } ## end if (Scalar::Util::blessed...) | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | (ref($d[$i]) eq "CODE") || die <<'HERE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | dispatch should be a code reference or an object that responds to dispatch() | ||||||||||||||||||||||||||||
| 85 | HERE | ||||||||||||||||||||||||||||
| 86 | } ## end for (my $i = 0; $i <= $#d...) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $self->dispatch([@d]); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $self; | |||||||||||||||||||||||||||
| 91 | } ## end sub new | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 call($env) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | delivers C<$env> to the dispatchers | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $self->_dispatch($env); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $self->app->($env); | |||||||||||||||||||||||||||
| 105 | } ## end sub call | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _dispatch { | ||||||||||||||||||||||||||
|
Subroutine
_dispatch: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($self, $o) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | foreach my $d (@{ $self->dispatch }) { | |||||||||||||||||||||||||||
| 110 | 0 | $d->($o); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } ## end sub _dispatch | ||||||||||||||||||||||||||||
| 113 | 1 | 1; # End of Plack::Middleware::Dispatch::GP | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Alexei Pastuchov, C<< <palik at cpan.org> >> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | L<https://github.com/p-alik/Plack-Middleware-Dispatch-GP> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | perldoc Plack::Middleware::Dispatch::GP | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Copyright 2015 Alexei Pastuchov. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 134 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 135 | copy of the full license at: | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 140 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 141 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 142 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 145 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 146 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 149 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 152 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 153 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 154 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 155 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 156 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 157 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 158 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 161 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 162 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 163 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 164 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 165 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 166 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 167 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||