DDG/Block/Blockable/Triggers.pm

filter: Statementstmt 95.9% 47 / 49 Branchbran 78.5% 22 / 28 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 3 totaltotal 85.4% 82 / 96 CC 20 SCAR 22.7
CC
20
Cov
89%
add_triggers
13.4
add
8.2
BEGIN
1.0
CRAP 9.7
« DDG/App/Attribution.pm ↑ Summary DDG/Meta/Data.pm »
1 package DDG::Block::Blockable::Triggers;
2 11 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: A package which reflects the triggers of a blockable plugin.
4 11 $DDG::Block::Blockable::Triggers::VERSION = '1017';
5 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our @words_types = qw(
9  
10 start
11 end
12 startend
13 any
14  
15 );
16  
17 11 our @regexp_types = qw(
18  
19 query_raw
20 query
21 query_lc
22 query_nowhitespace
23 query_nowhitespace_nodash
24 query_clean
25  
26 );
27  
28 11 our $default_regexp_type = 'query_raw';
29  
30 has triggers => (
31 is => 'ro',
32 27 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
33 11 );
34  
35 11 has block_type => (
36 is => 'rw',
37 predicate => 'has_block_type',
38 );
39  
40 35 ▶sub get { shift->triggers }
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
41  
42 sub add {
43 47 ▶ my ( $self, @args ) = @_;
Subroutine add: called CC=8 SCAR=21.0
Pod undocumented
44 47 my %params;
45 47 ▶ if (ref $args[0] eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 2 45 47
elsif (ref $args[0] eq 'HASH') { } 0 45 45
elsif (ref $args[0] eq 'Regexp') { } 7 38 45
46 2 %params = $args[0]->();
47 } elsif (ref $args[0] eq 'HASH') {
48 0 %params = %{$args[0]};
49 } elsif (ref $args[0] eq 'Regexp') {
50 7 %params = ( $default_regexp_type => [@args] );
51 } else {
52 38 ▶ if (scalar @args > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 2) { } 5 33 38
53 5 %params = ( $args[0] => [@args[1..(scalar @args-1)]] );
54 } else {
55 33 %params = ( $args[0] => $args[1] );
56 }
57 }
58 47 for (keys %params) {
59 47 my $trigger_type = $_;
60 47 ▶ my @triggers = ref $params{$trigger_type} eq 'ARRAY' ? @{$params{$trigger_type}} : ($params{$trigger_type});
Branch: 1 branch
BranchTrueFalseTotal
ref $params{$trigger_type} eq 'ARRAY' ? : 14 33 47
61 47 ▶ croak 'no trigger values given' unless @triggers;
Branch: 1 branch
BranchTrueFalseTotal
unless @triggers 0 47 47
62 47 $self->add_triggers($trigger_type, @triggers);
63 }
64 }
65  
66 sub add_triggers {
67 47 ▶ my ( $self, $trigger_type, @add_triggers ) = @_;
Subroutine add_triggers: called CC=13 SCAR=25.9
Pod undocumented
68 47 my @triggers;
69 47 for (@add_triggers) {
70 57 ▶ push @triggers, ref $_ eq 'CODE' ? $_->() : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 2 55 57
71 }
72 47 ▶ if (grep { $_ eq $trigger_type } @words_types) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq $trigger_type;} @DDG::Block::Blockable::Triggers::words_types) { } 39 8 47
elsif (grep {$_ eq $trigger_type;} @DDG::Block::Blockable::Triggers::regexp_types) { } 8 0 8
73 39 ▶ croak "You can't add trigger types of the other block-type" if $self->has_block_type && $self->block_type ne 'Words';
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_block_type and $self->block_type ne "Words" 0 39 39
Condition66%2/3
$self->has_block_type and $self->block_type ne "Words"
!ll&&!rl&&r
22170
MC/DC0%0/2
$self->has_block_type and $self->block_type ne "Words"
$self->has_block_type $self->block_type ne "Words"
Truth table66%2/3
A and B
A= $self->has_block_typeB= $self->block_type ne "Words"
ABresult
0X0
100
111
74 39 $self->block_type('Words');
75 48 } elsif (grep { $_ eq $trigger_type } @regexp_types) {
76 8 ▶ croak "You can't add trigger types of the other block-type" if $self->has_block_type && $self->block_type ne 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_block_type and $self->block_type ne "Regexp" 1 7 8
Condition100%3/3
$self->has_block_type and $self->block_type ne "Regexp"
!ll&&!rl&&r
521
MC/DC100%2/2
$self->has_block_type and $self->block_type ne "Regexp"
$self->has_block_type $self->block_type ne "Regexp"
Truth table100%3/3
A and B
A= $self->has_block_typeB= $self->block_type ne "Regexp"
ABresult
0X0
100
111
77 7 $self->block_type('Regexp');
78 7 for (@triggers) {
79 9 ▶ croak 'You may only give compiled regexps to regexp trigger types (like qr/reverse (.*)/i)' unless ref $_ eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "Regexp" 0 9 9
80 }
81 }
82 46 ▶ croak "your trigger_type '".$trigger_type."' is unknown" unless $self->has_block_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_block_type 0 46 46
83 46 ▶ $self->triggers->{$_} = [] unless defined $self->triggers->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->triggers->{$_} 35 11 46
84 46 push @{$self->triggers->{$_}}, @triggers;
85 }
86  
87 11 1;
88  
89 __END__
« DDG/App/Attribution.pm ↑ Summary DDG/Meta/Data.pm »