| 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 = '1016'; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 47 | ▶ | croak 'no trigger values given' unless @triggers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 47 | ▶ | if (grep { $_ eq $trigger_type } @words_types) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->has_block_type and $self->block_type ne "Words"
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"
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->has_block_type and $self->block_type ne "Regexp"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 46 | ▶ | croak "your trigger_type '".$trigger_type."' is unknown" unless $self->has_block_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 46 | ▶ | $self->triggers->{$_} = [] unless defined $self->triggers->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 46 | push @{$self->triggers->{$_}}, @triggers; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 11 | 1; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||