BusyBird/Util.pm

filter: Statementstmt 86.1% 143 / 166 Branchbran 75.9% 41 / 54 Conditioncond 77.7% 21 / 27 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 91.1% 31 / 34 Podpod 40.0% 4 / 10 totaltotal 81.1% 250 / 308 CC 42 SCAR 17.4
CC
42
Cov
83%
expand_param
20.0
split_with_entities
12.0
make_tracking
8.7
CRAP 5.7
« BusyBird/Runner.pm ↑ Summary BusyBird/Test/StatusStorage.pm »
1 package BusyBird::Util;
2 17 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Exporter 5.57 qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use BusyBird::Log qw(bblog);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use BusyBird::SafeData qw(safed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Future::Q 0.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 17 our @EXPORT_OK =
17 qw(set_param expand_param config_directory config_file_path sort_statuses
18 split_with_entities future_of make_tracking vivifiable_as);
19 17 our @CARP_NOT = qw(Future::Q);
20  
21 1616 ▶sub set_param {
Subroutine set_param: called CC=4 SCAR=15.2
Pod undocumented
22 1616 my ($hashref, $params_ref, $key, $default, $is_mandatory) = @_;
23 1616 ▶ if($is_mandatory && !defined($params_ref->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_mandatory and not defined $params_ref->{$key}) 0 1616 1616
Condition66%2/3
$is_mandatory and not defined $params_ref->{$key}
!ll&&!rl&&r
7818350
MC/DC0%0/2
$is_mandatory and not defined $params_ref->{$key}
$is_mandatory not defined $params_ref->{$key}
Truth table66%2/3
A and not(B)
A= $is_mandatoryB= not defined $params_ref->{$key}
ABresult
0X0
100
111
24 0 my $classname = blessed $hashref;
25 0 croak "ERROR: set_param in $classname: Parameter for '$key' is mandatory, but not supplied.";
26 }
27 1616 ▶ $hashref->{$key} = (defined($params_ref->{$key}) ? $params_ref->{$key} : $default);
Branch: 1 branch
BranchTrueFalseTotal
defined $params_ref->{$key} ? : 851 765 1616
28 }
29  
30 8 ▶sub export_ok_all_tags {
Subroutine export_ok_all_tags: called CC=2 SCAR=6.9
Pod undocumented
31 17 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 my ($caller_package) = caller;
33 8 my $export_ok = \@{"${caller_package}::EXPORT_OK"};
34 8 my $export_tags = \%{"${caller_package}::EXPORT_TAGS"};
35 8 my @all = @$export_ok;
36 8 foreach my $tag (keys %$export_tags) {
37 16 my $exported = $export_tags->{$tag};
38 16 push(@all, @$exported);
39 16 push(@$export_ok, @$exported);
40 }
41 8 $export_tags->{all} = \@all;
42 }
43  
44 0 ▶sub expand_param {
Subroutine expand_param: not called CC=4 SCAR=30.0
Pod undocumented
45 0 my ($param, @names) = @_;
46 0 my $refparam = ref($param);
47 0 my @result = ();
48 0 ▶ if($refparam eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($refparam eq 'ARRAY') { } 0 0 0
elsif ($refparam eq 'HASH') { } 0 0 0
49 0 @result = @$param;
50 }elsif($refparam eq 'HASH') {
51 0 @result = @{$param}{@names};
52 }else {
53 0 $result[0] = $param;
54 }
55 0 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
56 }
57  
58 0 ▶sub config_directory {
Subroutine config_directory: not called CC=1 SCAR=6.9
Pod undocumented
59 0 return File::Spec->catfile(File::HomeDir->my_home, ".busybird");
60 }
61  
62 0 ▶sub config_file_path {
Subroutine config_file_path: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my (@paths) = @_;
64 0 return File::Spec->catfile(config_directory, @paths);
65 }
66  
67 71 ▶sub vivifiable_as {
Subroutine vivifiable_as: called CC=2 SCAR=6.9
Pod undocumented
68 71 ▶ return !defined($_[0]) || ref($_[0]) eq $_[1];
Condition100%3/3
!defined($_[0]) || ref $_[0] eq $_[1]
l!l&&r!l&&!r
38303
MC/DC100%2/2
!defined($_[0]) || ref $_[0] eq $_[1]
!defined($_[0]) ref $_[0] eq $_[1]
Truth table100%3/3
not(A) || B
A= !defined($_[0])B= ref $_[0] eq $_[1]
ABresult
1X1
011
000
69 }
70  
71 2916 ▶sub _epoch_undef {
Subroutine _epoch_undef: called CC=2 SCAR=6.9
72 2916 my ($datetime_str) = @_;
73 2916 my $dt = BusyBird::DateTime::Format->parse_datetime($datetime_str);
74 2916 ▶ return defined($dt) ? $dt->epoch : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dt ? : 2152 764 2916
75 }
76  
77 3513 ▶sub _sort_compare {
Subroutine _sort_compare: called CC=7 SCAR=19.5
78 3513 my ($a, $b) = @_;
79 3513 ▶ if(defined($a) && defined($b)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $a and defined $b) { } 2355 1158 3513
elsif (not defined $a and defined $b) { } 73 1085 1158
elsif (defined $a and not defined $b) { } 222 863 1085
Condition100%3/3
defined $a and defined $b
!ll&&!rl&&r
9362222355
MC/DC100%2/2
defined $a and defined $b
defined $a defined $b
Truth table100%3/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
80 2355 return $b <=> $a;
81 ▶ }elsif(!defined($a) && defined($b)) {
Condition100%3/3
not defined $a and defined $b
!ll&&!rl&&r
22286373
MC/DC100%2/2
not defined $a and defined $b
not defined $a defined $b
Truth table100%3/3
not(A) and B
A= not defined $aB= defined $b
ABresult
0X0
100
111
82 73 return -1;
83 ▶ }elsif(defined($a) && !defined($b)) {
Condition66%2/3
defined $a and not defined $b
!ll&&!rl&&r
8630222
MC/DC50%1/2
defined $a and not defined $b
defined $a not defined $b
Truth table66%2/3
A and not(B)
A= defined $aB= not defined $b
ABresult
0X0
100
111
84 222 return 1;
85 }else {
86 863 return 0;
87 }
88 }
89  
90 92 ▶sub sort_statuses {
Subroutine sort_statuses: called CC=3 SCAR=11.0
Pod documented
91 92 my ($statuses) = @_;
92 17 ▶ use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
93
94 my @dt_statuses = map {
95 92 my $safe_status = safed($_);
96 [
97 1458 $_,
98 _epoch_undef($safe_status->val("busybird", "acked_at")),
99 _epoch_undef($safe_status->val("created_at")),
100 ];
101 } @$statuses;
102 1458 return [ map { $_->[0] } sort {
103 92 foreach my $sort_key (1, 2) {
104 3513 my $ret = _sort_compare($a->[$sort_key], $b->[$sort_key]);
105 3513 ▶ return $ret if $ret != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ret != 0 1897 1616 3513
106 }
107 61 return 0;
108 } @dt_statuses];
109 }
110  
111 94 ▶sub _create_text_segment {
Subroutine _create_text_segment: called CC=1 SCAR=0.0
112 return {
113 94 text => substr($_[0], $_[1], $_[2] - $_[1]),
114 start => $_[1],
115 end => $_[2],
116 type => $_[3],
117 entity => $_[4],
118 };
119 }
120  
121 32 ▶sub split_with_entities {
Subroutine split_with_entities: called CC=12 SCAR=24.8
Pod documented
122 32 my ($text, $entities_hashref) = @_;
123 17 ▶ use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 32 ▶ if(!defined($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 1 31 32
125 1 croak "text must not be undef";
126 }
127 31 ▶ if(ref($entities_hashref) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entities_hashref ne "HASH") 12 19 31
128 12 return [_create_text_segment($text, 0, length($text))];
129 }
130  
131 ## create entity segments
132 19 my @entity_segments = ();
133 19 foreach my $entity_type (keys %$entities_hashref) {
134 50 my $entities = $entities_hashref->{$entity_type};
135 50 ▶ next if ref($entities) ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $entities ne "ARRAY" 1 49 50
136 49 foreach my $entity (@$entities) {
137 46 my $se = safed($entity);
138 46 my $start = $se->val("indices", 0);
139 46 my $end = $se->val("indices", 1);
140 46 ▶ if(defined($start) && defined($end) && $start <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start and defined $end and $start <= $end) 41 5 46
Condition100%6/6
defined $start and defined $end and $start <= $end
!ll&&!rl&&r
4141
defined $start and defined $end
!ll&&!rl&&r
3142
MC/DC100%3/3
defined $start and defined $end and $start <= $end
defined $start defined $end $start <= $end
Truth table100%4/4
A and B and C
A= defined $startB= defined $endC= $start <= $end
ABCresult
0XX0
10X0
1100
1111
141 41 push(@entity_segments, _create_text_segment(
142 $text, $start, $end, $entity_type, $entity
143 ));
144 }
145 }
146 }
147 19 @entity_segments = sort { $a->{start} <=> $b->{start} } @entity_segments;
148  
149 ## combine entity_segments with non-entity segments
150 19 my $pos = 0;
151 19 my @final_segments = ();
152 19 foreach my $entity_segment (@entity_segments) {
153 41 ▶ if($pos < $entity_segment->{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $entity_segment->{"start"}) 30 11 41
154 push(@final_segments, _create_text_segment(
155 $text, $pos, $entity_segment->{start}
156 30 ));
157 }
158 41 push(@final_segments, $entity_segment);
159 41 $pos = $entity_segment->{end};
160 }
161 19 ▶ if($pos < length($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $text) 11 8 19
162 11 push(@final_segments, _create_text_segment(
163 $text, $pos, length($text)
164 ));
165 }
166 19 return \@final_segments;
167 }
168  
169 296 ▶sub future_of {
Subroutine future_of: called CC=1 SCAR=0.0
Pod documented
170 296 my ($invocant, $method, %args) = @_;
171 296 ▶ return Future::Q->try(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
172 296 ▶ croak "invocant parameter is mandatory" if not defined $invocant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $invocant 1 295 296
173 295 ▶ croak "method parameter is mandatory" if not defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 1 294 295
174 294 ▶ croak "invocant is not blessed" if not blessed $invocant;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($invocant) 1 293 294
175 293 ▶ croak "no such method as $method" if not $invocant->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $invocant->can($method) 1 292 293
176 292 my $f = Future::Q->new();
177 287 ▶ $invocant->$method(%args, callback => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
178 287 my ($error, @results) = @_;
179 287 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 6 281 287
180 6 $f->reject($error, 1);
181 }else {
182 281 $f->fulfill(@results);
183 }
184 292 });
185 287 return $f;
186 296 });
187 }
188  
189 2 ▶sub make_tracking {
Subroutine make_tracking: called CC=6 SCAR=21.6
Pod documented
190 2 my ($tracking_timeline, $main_timeline) = @_;
191 2 ▶ if(!blessed($tracking_timeline) || !$tracking_timeline->isa("BusyBird::Timeline")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($tracking_timeline) and $tracking_timeline->isa("BusyBird::Timeline")) 0 2 2
Condition33%1/3
builtin::blessed($tracking_timeline) and $tracking_timeline->isa("BusyBird::Timeline")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($tracking_timeline) and $tracking_timeline->isa("BusyBird::Timeline")
builtin::blessed($tracking_timeline) $tracking_timeline->isa("BusyBird::Timeline")
Truth table33%1/3
A and B
A= builtin::blessed($tracking_timeline)B= $tracking_timeline->isa("BusyBird::Timeline")
ABresult
0X0
100
111
192 0 croak "tracking_timeline must be a BusyBird::Timeline.";
193 }
194 2 ▶ if(!blessed($main_timeline) || !$main_timeline->isa("BusyBird::Timeline")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($main_timeline) and $main_timeline->isa("BusyBird::Timeline")) 0 2 2
Condition33%1/3
builtin::blessed($main_timeline) and $main_timeline->isa("BusyBird::Timeline")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($main_timeline) and $main_timeline->isa("BusyBird::Timeline")
builtin::blessed($main_timeline) $main_timeline->isa("BusyBird::Timeline")
Truth table33%1/3
A and B
A= builtin::blessed($main_timeline)B= $main_timeline->isa("BusyBird::Timeline")
ABresult
0X0
100
111
195 0 croak "main_timeline must be a BusyBird::Timeline.";
196 }
197 2 my $name_tracking = $tracking_timeline->name;
198 2 my $name_main = $main_timeline->name;
199 2 ▶ if($name_tracking eq $name_main) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_tracking eq $name_main) 0 2 2
200 0 croak "tracking_timeline and main_timeline must be different timelines.";
201 }
202 2 weaken(my $track = $tracking_timeline);
203 5 ▶ $tracking_timeline->add_filter_async(sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
204 5 my ($statuses, $done) = @_;
205 5 ▶ if(!defined($track)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $track) 0 5 5
206 0 $done->($statuses);
207 0 return;
208 }
209 5 ▶ $track->contains(query => $statuses, callback => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
210 5 my ($error, $contained, $not_contained) = @_;
211 5 ▶ if(defined($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 5 5
212 0 bblog("error", "tracking timeline '$name_tracking' contains() error: $error");
213 0 $done->($statuses);
214 0 return;
215 }
216 5 ▶ $main_timeline->add($not_contained, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
217 5 my ($error, $count) = @_;
218 5 ▶ if(defined($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 5 5
219 0 bblog("error", "main timeline '$name_main' add() error: $error");
220 }
221 5 $done->($statuses);
222 5 });
223 5 });
224 2 });
225 2 return $tracking_timeline;
226 }
227  
228 17 1;
229  
230 __END__
« BusyBird/Runner.pm ↑ Summary BusyBird/Test/StatusStorage.pm »