| 1 | package Smart::Dispatch; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 8 | 5 | ▶ | use Smart::Dispatch::Table (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 5 | ▶ | use Smart::Dispatch::Match (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||
| 11 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 12 | 5 | $Smart::Dispatch::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||
| 13 | 5 | $Smart::Dispatch::VERSION = '0.006'; | |||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | 5 | ▶ | use constant DEFAULT_MATCH_CLASS => (__PACKAGE__.'::Match'); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 5 | ▶ | use constant DEFAULT_TABLE_CLASS => (__PACKAGE__.'::Table'); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||
| 19 | 5 | our ($IN_FLIGHT, @LIST, @EXPORT); | |||||||||||||||||||||||
| 20 | BEGIN | ||||||||||||||||||||||||
| 21 | 5 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||
| 22 | $Carp::Internal{$_}++ | ||||||||||||||||||||||||
| 23 | 5 | foreach (__PACKAGE__, DEFAULT_MATCH_CLASS, DEFAULT_TABLE_CLASS); | |||||||||||||||||||||||
| 24 | 5 | $IN_FLIGHT = 0; | |||||||||||||||||||||||
| 25 | 5 | @LIST = (); | |||||||||||||||||||||||
| 26 | 5 | @EXPORT = qw/dispatcher match match_using otherwise dispatch failover/; | |||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||
| 29 | 5 | ▶ | use namespace::clean (); | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 30 | 5 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 31 | exports => [ | ||||||||||||||||||||||||
| 32 | dispatcher => \&_build_dispatcher, | ||||||||||||||||||||||||
| 33 | match => \&_build_match, | ||||||||||||||||||||||||
| 34 | match_using => \&_build_match_using, | ||||||||||||||||||||||||
| 35 | otherwise => \&_build_otherwise, | ||||||||||||||||||||||||
| 36 | dispatch => \&_build_dispatch, | ||||||||||||||||||||||||
| 37 | failover => \&_build_failover, | ||||||||||||||||||||||||
| 38 | ], | ||||||||||||||||||||||||
| 39 | groups => [ | ||||||||||||||||||||||||
| 40 | default => [@EXPORT], | ||||||||||||||||||||||||
| 41 | tiny => [qw/dispatcher match/], | ||||||||||||||||||||||||
| 42 | ], | ||||||||||||||||||||||||
| 43 | collectors => [qw/class/], | ||||||||||||||||||||||||
| 44 | 5 | ▶ | installer => sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 45 | namespace::clean::->import( | ||||||||||||||||||||||||
| 46 | -cleanee => $_[0]{into}, | ||||||||||||||||||||||||
| 47 | 5 | grep { !ref } @{ $_[1] }, | |||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||
| 49 | 5 | goto \&Sub::Exporter::default_installer; | |||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||
| 51 | 5 | }; | |||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||
| 53 | sub _build_dispatcher | ||||||||||||||||||||||||
| 54 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_dispatcher: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||
| 55 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 56 | my $table_class = | ||||||||||||||||||||||||
| 57 | $arg->{class} | ||||||||||||||||||||||||
| 58 | // $col->{class}{table} | ||||||||||||||||||||||||
| 59 | 10 | ▶ | // DEFAULT_TABLE_CLASS; | ||||||||||||||||||||||
|
Condition80%4/5
($arg->{"class"} // $col->{"class"}{"table"}) // "Smart::Dispatch::Table"
$arg->{"class"} // $col->{"class"}{"table"}
MC/DC50%1/2
($arg->{"class"} // $col->{"class"}{"table"}) // "Smart::Dispatch::Table"
$arg->{"class"}
$col->{"class"}{"table"}
Truth table66%2/3
A // B
A= $arg->{"class"}B= $col->{"class"}{"table"}
| |||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||
| 61 | return sub (&) | ||||||||||||||||||||||||
| 62 | 4 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 63 | 4 | my $body = shift; | |||||||||||||||||||||||
| 64 | 4 | local @LIST = (); | |||||||||||||||||||||||
| 65 | 4 | local $IN_FLIGHT = 1; | |||||||||||||||||||||||
| 66 | 4 | $body->(); | |||||||||||||||||||||||
| 67 | 4 | return $table_class->new(match_list => [@LIST]); | |||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||
| 69 | 10 | } | |||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||
| 71 | sub _build_match | ||||||||||||||||||||||||
| 72 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_match: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||
| 73 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 74 | my $match_class = | ||||||||||||||||||||||||
| 75 | $arg->{class} | ||||||||||||||||||||||||
| 76 | // $col->{class}{match} | ||||||||||||||||||||||||
| 77 | 10 | ▶ | // DEFAULT_MATCH_CLASS; | ||||||||||||||||||||||
|
Condition80%4/5
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"} // $col->{"class"}{"match"}
MC/DC50%1/2
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"}
$col->{"class"}{"match"}
Truth table66%2/3
A // B
A= $arg->{"class"}B= $col->{"class"}{"match"}
| |||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||
| 79 | return sub | ||||||||||||||||||||||||
| 80 | 13 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||
| 81 | 13 | ▶ | croak "$name cannot be used outside dispatcher" unless $IN_FLIGHT; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 82 | 13 | ▶ | my ($condition, %args) = (@_ == 2) ? (shift, _k($_[-1]), shift) : (@_); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 83 | 13 | push @LIST, $match_class->new(%args, test => $condition); | |||||||||||||||||||||||
| 84 | 13 | return; | |||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||
| 86 | 10 | } | |||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||
| 88 | sub _build_match_using | ||||||||||||||||||||||||
| 89 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_match_using: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||
| 90 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 91 | my $match_class = | ||||||||||||||||||||||||
| 92 | $arg->{class} | ||||||||||||||||||||||||
| 93 | // $col->{class}{match} | ||||||||||||||||||||||||
| 94 | 10 | ▶ | // DEFAULT_MATCH_CLASS; | ||||||||||||||||||||||
|
Condition80%4/5
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"} // $col->{"class"}{"match"}
MC/DC50%1/2
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"}
$col->{"class"}{"match"}
Truth table66%2/3
A // B
A= $arg->{"class"}B= $col->{"class"}{"match"}
| |||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||
| 96 | return sub (&@) | ||||||||||||||||||||||||
| 97 | 5 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||
| 98 | 5 | ▶ | croak "$name cannot be used outside dispatcher" unless $IN_FLIGHT; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 99 | 5 | ▶ | my ($condition, %args) = (@_ == 2) ? (shift, _k($_[-1]), shift) : (@_); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 100 | 5 | push @LIST, $match_class->new(%args, test => $condition); | |||||||||||||||||||||||
| 101 | 5 | return; | |||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||
| 103 | 10 | } | |||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||
| 105 | sub _build_otherwise | ||||||||||||||||||||||||
| 106 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_otherwise: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||
| 107 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 108 | my $match_class = | ||||||||||||||||||||||||
| 109 | $arg->{class} | ||||||||||||||||||||||||
| 110 | // $col->{class}{match} | ||||||||||||||||||||||||
| 111 | 10 | ▶ | // DEFAULT_MATCH_CLASS; | ||||||||||||||||||||||
|
Condition80%4/5
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"} // $col->{"class"}{"match"}
MC/DC50%1/2
($arg->{"class"} // $col->{"class"}{"match"}) // "Smart::Dispatch::Match"
$arg->{"class"}
$col->{"class"}{"match"}
Truth table66%2/3
A // B
A= $arg->{"class"}B= $col->{"class"}{"match"}
| |||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||
| 113 | return sub | ||||||||||||||||||||||||
| 114 | 2 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||
| 115 | 2 | ▶ | croak "$name cannot be used outside dispatcher" unless $IN_FLIGHT; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 116 | 2 | ▶ | my (%args) = (@_ == 1) ? (_k($_[-1]), shift) : (@_); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 117 | 2 | ▶ | push @LIST, $match_class->new(%args, is_unconditional => 1, test => sub {1}); | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 118 | 2 | return; | |||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||
| 120 | 10 | } | |||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||
| 122 | sub _build_dispatch | ||||||||||||||||||||||||
| 123 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_dispatch: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 124 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||
| 126 | return sub (&) | ||||||||||||||||||||||||
| 127 | 14 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||
| 128 | 14 | ▶ | croak "$name cannot be used outside dispatcher" unless $IN_FLIGHT; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 129 | 14 | return('dispatch', shift); | |||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||
| 131 | 10 | } | |||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||
| 133 | sub _build_failover | ||||||||||||||||||||||||
| 134 | 10 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_build_failover: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 135 | 10 | my ($class, $name, $arg, $col) = @_; | |||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||
| 137 | return sub (&) | ||||||||||||||||||||||||
| 138 | 4 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||
| 139 | 4 | ▶ | croak "$name cannot be used outside dispatcher" unless $IN_FLIGHT; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 140 | 4 | return('dispatch', shift, is_failover => 1); | |||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||
| 142 | 10 | } | |||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||
| 144 | sub _k | ||||||||||||||||||||||||
| 145 | 0 | ▶ | { | ||||||||||||||||||||||
|
Subroutine
_k: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 146 | 0 | ▶ | ref $_[0] eq 'CODE' ? 'dispatch' : 'value'; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||
| 149 | 5 | foreach my $f (@EXPORT) | |||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||
| 151 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 152 | 30 | *{"$f"} = &{"_build_$f"}(__PACKAGE__, $f, {}, {}); | |||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||
| 155 | __PACKAGE__ | ||||||||||||||||||||||||
| 156 | 5 | __END__ | |||||||||||||||||||||||