ChatBots/Role/Source.pm

filter: Statementstmt 90.4% 66 / 73 Branchbran 46.6% 14 / 30 Conditioncond 45.0% 9 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 82.3% 14 / 17 Podpod 100% 6 / 6 totaltotal 70.2% 111 / 158 CC 25 SCAR 23.3
CC
25
Cov
72%
should_rethrow
30.0
process_updates
16.8
pack_source
7.0
CRAP 10.3
« ChatBots/Utils.pm ↑ Summary ChatBots/Auth.pm »
1 package Bot::ChatBots::Role::Source;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 { our $VERSION = '0.014'; }
5  
6 5 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Log::Any qw< $log >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Bot::ChatBots::Weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 requires 'normalize_record';
15  
16 has custom_pairs => (
17 is => 'rw',
18 3 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 5 );
20  
21 5 has processor => (
22 is => 'ro',
23 lazy => 1,
24 builder => 'BUILD_processor',
25 );
26  
27 5 has typename => (
28 is => 'ro',
29 lazy => 1,
30 builder => 'BUILD_typename',
31 );
32  
33 0 ▶sub BUILD_processor {
Subroutine BUILD_processor: not called CC=1 SCAR=6.9
Pod documented
34 0 ouch 500, 'no processor defined!';
35 }
36  
37 3 ▶sub BUILD_typename {
Subroutine BUILD_typename: called CC=3 SCAR=12.5
Pod documented
38 3 my $self = shift;
39 3 my @chunks = split /::/, lc ref $self;
40 return (
41 3 ▶ ((@chunks == 1) || ($chunks[-1] ne 'webhook'))
Branch: 1 branch
BranchTrueFalseTotal
@chunks == 1 || $chunks[-1] ne 'webhook' ? : 3 0 3
Condition33%1/3
@chunks == 1 or $chunks[-1] ne "webhook"
l!l&&r!l&&!r
030
MC/DC0%0/2
@chunks == 1 or $chunks[-1] ne "webhook"
@chunks == 1 $chunks[-1] ne "webhook"
Truth table33%1/3
A or B
A= @chunks == 1B= $chunks[-1] ne "webhook"
ABresult
1X1
011
000
42 ? $chunks[-1]
43 : $chunks[-2]
44 );
45 } ## end sub BUILD_typename
46  
47 5 ▶sub pack_source {
Subroutine pack_source: called CC=7 SCAR=19.5
Pod documented
48 5 my $self = shift;
49 5 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 4 1 5
Condition66%2/3
@_ and ref $_[0]
!ll&&!rl&&r
014
MC/DC50%1/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table66%2/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
50  
51 5 ▶ my %class_custom_pairs =
Branch: 1 branch
BranchTrueFalseTotal
$self->can('class_custom_pairs') ? : 3 2 5
52 $self->can('class_custom_pairs')
53 ? $self->class_custom_pairs
54 : ();
55  
56 5 my $refs = Bot::ChatBots::Weak->new;
57 5 $refs->set(self => $self);
58 5 ▶ while (my ($k, $v) = each %{$args->{refs} // {}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args->{"refs"} // {};}) 12 5 17
Condition100%2/2
$args->{"refs"} // {}
l!l
161
MC/DC100%1/1
$args->{"refs"} // {}
$args->{"refs"}
Truth table100%2/2
A
A= $args->{"refs"}
Aresult
11
00
59 12 $refs->set($k, $v);
60 }
61  
62 my $source = {
63 class => ref($self),
64 refs => $refs,
65 type => $self->typename,
66 %class_custom_pairs,
67 5 %{$self->custom_pairs},
68 5 ▶ %{$args->{source_pairs} // {}},
Condition50%1/2
$args->{"source_pairs"} // {}
l!l
50
MC/DC0%0/1
$args->{"source_pairs"} // {}
$args->{"source_pairs"}
Truth table50%1/2
A
A= $args->{"source_pairs"}
Aresult
11
00
69 };
70  
71 5 return $source;
72 } ## end sub pack_source
73  
74 5 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
75 5 my $self = shift;
76 5 return $self->processor->(@_);
77 }
78  
79 4 ▶sub process_updates {
Subroutine process_updates: called CC=11 SCAR=28.2
Pod documented
80 4 my $self = shift;
81 4 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 4 4
Condition33%1/3
@_ and ref $_[0]
!ll&&!rl&&r
040
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table33%1/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
82  
83 4 ▶ my $updates = $args->{updates} // [];
Condition50%1/2
$args->{"updates"} // []
l!l
40
MC/DC0%0/1
$args->{"updates"} // []
$args->{"updates"}
Truth table50%1/2
A
A= $args->{"updates"}
Aresult
11
00
84 4 ▶ ouch 500, 'updates is not an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $updates eq "ARRAY" 0 4 4
85 unless ref($updates) eq 'ARRAY';
86 4 ▶ my $n_updates = @$updates or return;
Branch: 1 branch
BranchTrueFalseTotal
my $n_updates = @$updates or return 4 0 4
87  
88 4 my $source = $self->pack_source($args);
89  
90 4 my @retval;
91 4 for my $i (0 .. ($n_updates - 1)) {
92 4 my %call;
93 4 ▶ push @retval, \%call if defined wantarray; # no void context
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 4 0 4
94 4 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.0
95 4 $call{update} = $updates->[$i];
96 $call{record} = $self->normalize_record(
97 {
98 batch => {
99 count => ($i + 1),
100 total => $n_updates,
101 },
102 source => $source,
103 update => $call{update},
104 4 ▶ %{$args->{record_pairs} // {}},
Condition50%1/2
$args->{"record_pairs"} // {}
l!l
04
MC/DC0%0/1
$args->{"record_pairs"} // {}
$args->{"record_pairs"}
Truth table50%1/2
A
A= $args->{"record_pairs"}
Aresult
11
00
105 }
106 );
107 4 $call{outcome} = $self->process($call{record});
108 } ## end try
109 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
110 0 $log->error(bleep $_);
111 0 ▶ die $_ if $self->should_rethrow($args);
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_rethrow($args) 0 0 0
112 4 };
113 } ## end for my $i (0 .. ($n_updates...))
114  
115 4 ▶ $self->review_outcomes(@retval) if $self->can('review_outcomes');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("review_outcomes") 0 4 4
116  
117 4 ▶ return unless defined wantarray; # void is void!
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 4 4
118 4 ▶ return @retval if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 4 0 4
119 0 return \@retval;
120 } ## end sub process_updates
121  
122 0 ▶sub should_rethrow {
Subroutine should_rethrow: not called CC=5 SCAR=34.0
Pod documented
123 0 my $self = shift;
124 0 ▶ my $args = (@_ && ref($_[0])) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
125 return
126 exists($args->{rethrow}) ? $args->{rethrow}
127 0 ▶ : $self->can('rethrow') ? $self->rethrow
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'rethrow'} ? : 0 0 0
$self->can('rethrow') ? : 0 0 0
128 : 0;
129 } ## end sub should_rethrow
130  
131 5 1;
« ChatBots/Utils.pm ↑ Summary ChatBots/Auth.pm »