| 1 | package DDG::Block; | ||||||||||||||||||||||||||||
| 2 | 10 | our $AUTHORITY = 'cpan:DDG'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Block to bundle plugins with triggers | ||||||||||||||||||||||||||||
| 4 | 10 | $DDG::Block::VERSION = '1017'; | |||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Class::Load ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | requires qw( | |||||||||||||||||||||||||||
| 11 | request | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub BUILD { | ||||||||||||||||||||||||||||
| 15 | 3 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 3 | $self->_plugin_objs; | |||||||||||||||||||||||||||
| 17 | $self->trace('Loaded block', | ||||||||||||||||||||||||||||
| 18 | ref $self, | ||||||||||||||||||||||||||||
| 19 | map { | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | $_.':'.( $self->$_ ? 1 : 0 ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } qw( return_one allow_missing_plugins allow_duplicate ), | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 10 | has plugins => ( | |||||||||||||||||||||||||||
| 27 | #isa => 'ArrayRef[Str|HashRef]', | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||||||||||
| 30 | builder => '_build_plugins', | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _build_plugins { die (ref shift)." requires plugins" } | ||||||||||||||||||||||||||
|
Subroutine
_build_plugins: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | has allow_missing_plugins => ( | ||||||||||||||||||||||||||||
| 37 | #isa => 'Bool', | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | 14 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 10 | ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | has return_one => ( | ||||||||||||||||||||||||||||
| 44 | #isa => 'Bool', | ||||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | 13 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 10 | ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | has allow_duplicate => ( | ||||||||||||||||||||||||||||
| 51 | #isa => 'Bool', | ||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||
| 53 | 16 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 10 | ); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | has debug_trace => ( | ||||||||||||||||||||||||||||
| 58 | #isa => 'Bool', | ||||||||||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||||||||||
| 60 | 16 | ▶ | default => sub { defined $ENV{DDG_BLOCK_TRACE} && $ENV{DDG_BLOCK_TRACE} ? 1 : 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $ENV{"DDG_BLOCK_TRACE"} and $ENV{"DDG_BLOCK_TRACE"}
MC/DC0%0/2
defined $ENV{"DDG_BLOCK_TRACE"} and $ENV{"DDG_BLOCK_TRACE"}
defined $ENV{"DDG_BLOCK_TRACE"}
$ENV{"DDG_BLOCK_TRACE"}
Truth table33%1/3
A and B
A= defined $ENV{"DDG_BLOCK_TRACE"}B= $ENV{"DDG_BLOCK_TRACE"}
| |||||||||||||||||||||||||||||
| 61 | 10 | ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub trace { | ||||||||||||||||||||||||||||
| 64 | 596 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
trace: called CC=3 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 596 | ▶ | return unless $self->debug_trace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | print STDERR ("[".$self->trace_name."] ",join(" ",map { defined $_ ? $_ : 'undef' } @_),"\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub trace_name { ref(shift) } | ||||||||||||||||||||||||||
|
Subroutine
trace_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 10 | has before_build => ( | |||||||||||||||||||||||||||
| 73 | #isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 74 | is => 'ro', | ||||||||||||||||||||||||||||
| 75 | predicate => 'has_before_build', | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 10 | has after_build => ( | |||||||||||||||||||||||||||
| 80 | #isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||||||||||
| 82 | predicate => 'has_after_build', | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 10 | has _plugin_objs => ( | |||||||||||||||||||||||||||
| 87 | # like ArrayRef[ArrayRef[$trigger,DDG::Block::Plugin]]', | ||||||||||||||||||||||||||||
| 88 | is => 'ro', | ||||||||||||||||||||||||||||
| 89 | lazy => 1, | ||||||||||||||||||||||||||||
| 90 | builder => '_build__plugin_objs', | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | 34 | ▶ | sub plugin_objs { shift->_plugin_objs } | ||||||||||||||||||||||||||
|
Subroutine
plugin_objs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub _build__plugin_objs { | ||||||||||||||||||||||||||||
| 95 | 16 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__plugin_objs: called CC=15 SCAR=27.9
| |||||||||||||||||||||||||||||
| 96 | 16 | my @plugin_objs; | |||||||||||||||||||||||||||
| 97 | 16 | for (@{$self->plugins}) { | |||||||||||||||||||||||||||
| 98 | 36 | my $class; | |||||||||||||||||||||||||||
| 99 | my %args; | ||||||||||||||||||||||||||||
| 100 | 36 | ▶ | if (ref $_ eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | croak "require a class key in hash" unless defined $_->{class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | $class = delete $_->{class}; | |||||||||||||||||||||||||||
| 103 | 0 | %args = %{$_}; | |||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||
| 105 | 36 | $class = $_; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 36 | my $plugin; | |||||||||||||||||||||||||||
| 108 | 36 | ▶ | if (ref $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $plugin = $class; | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 36 | ▶ | unless (try_load_class($class)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | if ($self->allow_missing_plugins) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | if (ref $self->allow_missing_plugins eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | $self->allow_missing_plugins->($self,$class); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 1 | die "Can't load plugin ".$class; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 2 | next; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 33 | $args{block} = $self; | |||||||||||||||||||||||||||
| 122 | 33 | ▶ | if ($self->has_before_build) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 8 | for ($class) { | |||||||||||||||||||||||||||
| 124 | 8 | $self->before_build->($self,$class); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 33 | $plugin = $class->new(\%args); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 33 | ▶ | if ($self->has_after_build) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 8 | for ($plugin) { | |||||||||||||||||||||||||||
| 131 | 8 | $self->after_build->($self,$plugin); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 33 | my @triggers = $self->get_triggers_of_plugin($plugin); | |||||||||||||||||||||||||||
| 135 | 33 | ▶ | @triggers = $self->empty_trigger unless @triggers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 33 | my @parsed_triggers; | |||||||||||||||||||||||||||
| 137 | 33 | for (@triggers) { | |||||||||||||||||||||||||||
| 138 | 33 | push @parsed_triggers, $self->parse_trigger($_); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 33 | ▶ | push @plugin_objs, [ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | \@parsed_triggers, | ||||||||||||||||||||||||||||
| 142 | $plugin, | ||||||||||||||||||||||||||||
| 143 | ] if @parsed_triggers; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 15 | return \@plugin_objs; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 10 | has only_plugin_objs => ( | |||||||||||||||||||||||||||
| 150 | is => 'ro', | ||||||||||||||||||||||||||||
| 151 | lazy => 1, | ||||||||||||||||||||||||||||
| 152 | builder => '_build_only_plugin_objs', | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | sub _build_only_plugin_objs { | ||||||||||||||||||||||||||||
| 155 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_only_plugin_objs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 156 | 1 | my @plugins; | |||||||||||||||||||||||||||
| 157 | 1 | for (@{$self->_plugin_objs}) { | |||||||||||||||||||||||||||
| 158 | 2 | push @plugins, $_->[1]; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 1 | return \@plugins; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 31 | ▶ | sub get_triggers_of_plugin { shift; shift->get_triggers } | ||||||||||||||||||||||||||
|
Subroutine
get_triggers_of_plugin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 30 | ▶ | sub parse_trigger { shift; shift; } | ||||||||||||||||||||||||||
|
Subroutine
parse_trigger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 2 | ▶ | sub empty_trigger { return undef } | ||||||||||||||||||||||||||
|
Subroutine
empty_trigger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub handle_request_matches { | ||||||||||||||||||||||||||||
| 174 | 46 | ▶ | my ( $self, $plugin, $request, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
handle_request_matches: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 46 | my $plugin_class = ref $plugin; | |||||||||||||||||||||||||||
| 176 | 46 | $self->trace('Handle request matches:',$plugin_class,"'".$request->query_raw."'",@args); | |||||||||||||||||||||||||||
| 177 | 46 | ▶ | unless ($self->allow_duplicate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 46 | ▶ | if (grep { $_ eq $plugin_class } @{$request->seen_plugins}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | $self->trace("The request already saw",$plugin_class); | |||||||||||||||||||||||||||
| 180 | 1 | return (); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 45 | push @{$request->seen_plugins}, $plugin_class; | |||||||||||||||||||||||||||
| 184 | 45 | my @results = $plugin->handle_request_matches($request, @args); | |||||||||||||||||||||||||||
| 185 | 45 | $self->trace("Got",scalar @results,"results"); | |||||||||||||||||||||||||||
| 186 | 45 | return @results; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | around request => sub { | ||||||||||||||||||||||||||||
| 190 | 64 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 191 | 64 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||
| 192 | 64 | $self->trace( "Query raw:", "'".$request->query_raw."'" ); | |||||||||||||||||||||||||||
| 193 | 64 | my @results = $orig->(@_); | |||||||||||||||||||||||||||
| 194 | 64 | $self->trace( "Query", "'".$request->query_raw."'", "produced", scalar @results, "results" ); | |||||||||||||||||||||||||||
| 195 | 64 | return @results; | |||||||||||||||||||||||||||
| 196 | 10 | }; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 10 | 1; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||||||||||