| 1 | package Smart::Dispatch::Table; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 4 | *_TYPES = $ENV{PERL_SMART_DISPATCH_TYPE_CHECKS}==42 | ||||||||||||||||||||||||||||
| 5 | ? sub () { 1 } | ||||||||||||||||||||||||||||
| 6 | 5 | ▶ | : sub () { 0 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 7 | }; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Scalar::Util qw/ refaddr blessed /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use if _TYPES, 'MooX::Types::MooseLike::Base', ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub _swap | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | my ($x, $y, $swap) = @_; | ||||||||||||||||||||||||||||
| 18 | $swap ? ($y, $x) : ($x, $y); | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | $Smart::Dispatch::Table::AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 25 | 5 | $Smart::Dispatch::Table::VERSION = '0.006'; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | use overload | ||||||||||||||||||||||||||||
| 29 | 6 | ▶ | '&{}' => sub { my $x=shift; sub { $x->action($_[0]) } }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | '+' => sub { __PACKAGE__->make_combined(reverse _swap(@_)) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | '.' => sub { __PACKAGE__->make_combined(_swap(@_)) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | '+=' => 'prepend', | ||||||||||||||||||||||||||||
| 33 | '.=' => 'append', | ||||||||||||||||||||||||||||
| 34 | '~~' => 'exists', | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 5 | ; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | has match_list => ( | |||||||||||||||||||||||||||
| 39 | (_TYPES?(isa=>ArrayRef()):()), | ||||||||||||||||||||||||||||
| 40 | is => 'rw', | ||||||||||||||||||||||||||||
| 41 | required => 1, | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub BUILD | ||||||||||||||||||||||||||||
| 45 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 47 | 4 | $self->validate_match_list; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub make_combined | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
make_combined: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($class, @all) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | my $self = $class->new(match_list => []); | |||||||||||||||||||||||||||
| 54 | 0 | $self->append(@all); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub validate_match_list | ||||||||||||||||||||||||||||
| 58 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
validate_match_list: called CC=4 SCAR=15.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 60 | 4 | my @otherwise = $self->unconditional_matches; | |||||||||||||||||||||||||||
| 61 | 4 | ▶ | if (scalar @otherwise > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | carp "Too many 'otherwise' matches. Only one allowed."; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 4 | ▶ | if (@otherwise and refaddr($otherwise[0]) != refaddr($self->match_list->[-1])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@otherwise and builtin::refaddr($otherwise[0]) != builtin::refaddr($self->match_list->[-1])
MC/DC0%0/2
@otherwise and builtin::refaddr($otherwise[0]) != builtin::refaddr($self->match_list->[-1])
@otherwise
builtin::refaddr($otherwise[0]) != builtin::refaddr($self->match_list->[-1])
Truth table66%2/3
A and B
A= @otherwiseB= builtin::refaddr($otherwise[0]) != builtin::refaddr($self->match_list->[-1])
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | carp "The 'otherwise' match is not the last match."; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub all_matches | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
all_matches: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 1 | @{ $self->match_list }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub unconditional_matches | ||||||||||||||||||||||||||||
| 78 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unconditional_matches: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 80 | 5 | grep { $_->is_unconditional } @{ $self->match_list }; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub conditional_matches | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
conditional_matches: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 86 | 1 | grep { !$_->is_unconditional } @{ $self->match_list }; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub exists | ||||||||||||||||||||||||||||
| 90 | 17 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 17 | my ($self, $value, $allow_fails) = @_; | |||||||||||||||||||||||||||
| 92 | 17 | foreach my $cond (@{ $self->match_list }) | |||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 62 | ▶ | if ($cond->value_matches($value)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 13 | ▶ | if ($allow_fails or not $cond->is_failover) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$allow_fails or not $cond->is_failover
MC/DC100%2/2
$allow_fails or not $cond->is_failover
$allow_fails
not $cond->is_failover
Truth table100%3/3
A or not(B)
A= $allow_failsB= not $cond->is_failover
| |||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 12 | return $cond; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else | ||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 1 | return; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 4 | return; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub action | ||||||||||||||||||||||||||||
| 110 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
action: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 10 | my ($self, $value, @args) = @_; | |||||||||||||||||||||||||||
| 112 | 10 | my $cond = $self->exists($value, 1); | |||||||||||||||||||||||||||
| 113 | 10 | ▶ | return $cond->conduct_dispatch($value, @args) if $cond; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub append | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
append: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 0 | foreach my $other (@_) | |||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | next unless defined $other; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | carp "Cannot add non-reference to dispatch table" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | unless ref $other; | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | carp "Cannot add non-blessed reference to dispatch table" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | unless blessed $other; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($other->isa(__PACKAGE__)) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $self->match_list([ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 132 | $other->conditional_matches, | ||||||||||||||||||||||||||||
| 133 | ($self->unconditional_matches ? $self->unconditional_matches : $other->unconditional_matches), | ||||||||||||||||||||||||||||
| 134 | ]); | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | ▶ | elsif ($other->isa('Smart::Dispatch::Match') | |||||||||||||||||||||||||||
|
Condition0%0/3
$other->isa("Smart::Dispatch::Match") and not $other->is_unconditional
MC/DC0%0/2
$other->isa("Smart::Dispatch::Match") and not $other->is_unconditional
$other->isa("Smart::Dispatch::Match")
not $other->is_unconditional
Truth table0%0/3
A and not(B)
A= $other->isa("Smart::Dispatch::Match")B= not $other->is_unconditional
| |||||||||||||||||||||||||||||
| 137 | and not $other->is_unconditional) | ||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 0 | $self->match_list([ | |||||||||||||||||||||||||||
| 140 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 141 | $other, | ||||||||||||||||||||||||||||
| 142 | $self->unconditional_matches, | ||||||||||||||||||||||||||||
| 143 | ]); | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | ▶ | elsif ($other->isa('Smart::Dispatch::Match') | |||||||||||||||||||||||||||
|
Condition0%0/3
$other->isa("Smart::Dispatch::Match") and $other->is_unconditional
MC/DC0%0/2
$other->isa("Smart::Dispatch::Match") and $other->is_unconditional
$other->isa("Smart::Dispatch::Match")
$other->is_unconditional
Truth table0%0/3
A and B
A= $other->isa("Smart::Dispatch::Match")B= $other->is_unconditional
| |||||||||||||||||||||||||||||
| 146 | and $other->is_unconditional) | ||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $self->match_list([ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 150 | ($self->unconditional_matches ? $self->conditional_matches : $other), | ||||||||||||||||||||||||||||
| 151 | ]); | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | else | ||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 0 | carp sprintf("Cannot add object of type '%s' to dispatch table", ref $other); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | $self->validate_match_list; | |||||||||||||||||||||||||||
| 160 | 0 | return $self; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub prepend | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prepend: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 166 | 0 | foreach my $other (@_) | |||||||||||||||||||||||||||
| 167 | { | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | next unless defined $other; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | carp "Cannot add non-reference to dispatch table" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | unless ref $other; | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | carp "Cannot add non-blessed reference to dispatch table" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | unless blessed $other; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ($other->isa(__PACKAGE__)) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | $self->match_list([ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | $other->conditional_matches, | ||||||||||||||||||||||||||||
| 178 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 179 | ($other->unconditional_matches ? $other->unconditional_matches : $self->unconditional_matches), | ||||||||||||||||||||||||||||
| 180 | ]); | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | ▶ | elsif ($other->isa('Smart::Dispatch::Match') | |||||||||||||||||||||||||||
|
Condition0%0/3
$other->isa("Smart::Dispatch::Match") and not $other->is_unconditional
MC/DC0%0/2
$other->isa("Smart::Dispatch::Match") and not $other->is_unconditional
$other->isa("Smart::Dispatch::Match")
not $other->is_unconditional
Truth table0%0/3
A and not(B)
A= $other->isa("Smart::Dispatch::Match")B= not $other->is_unconditional
| |||||||||||||||||||||||||||||
| 183 | and not $other->is_unconditional) | ||||||||||||||||||||||||||||
| 184 | { | ||||||||||||||||||||||||||||
| 185 | 0 | $self->conditions([ | |||||||||||||||||||||||||||
| 186 | $other, | ||||||||||||||||||||||||||||
| 187 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 188 | $self->unconditional_matches, | ||||||||||||||||||||||||||||
| 189 | ]); | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | ▶ | elsif ($other->isa('Smart::Dispatch::Match') | |||||||||||||||||||||||||||
|
Condition0%0/3
$other->isa("Smart::Dispatch::Match") and $other->is_unconditional
MC/DC0%0/2
$other->isa("Smart::Dispatch::Match") and $other->is_unconditional
$other->isa("Smart::Dispatch::Match")
$other->is_unconditional
Truth table0%0/3
A and B
A= $other->isa("Smart::Dispatch::Match")B= $other->is_unconditional
| |||||||||||||||||||||||||||||
| 192 | and $other->is_unconditional) | ||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||
| 194 | 0 | $self->conditions([ | |||||||||||||||||||||||||||
| 195 | $self->conditional_matches, | ||||||||||||||||||||||||||||
| 196 | $other, | ||||||||||||||||||||||||||||
| 197 | ]); | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | else | ||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | 0 | carp sprintf("Cannot add object of type '%s' to dispatch table", ref $other); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | $self->validate_match_list; | |||||||||||||||||||||||||||
| 206 | 0 | return $self; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 5 | __PACKAGE__ | |||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||