App/Chart/Gtk2/Job/Latest.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 139 CC 8 SCAR 17.8
CC
8
Cov
0%
start_for_view
12.0
start
12.0
_do_notify
12.0
CRAP 5.9
1 # Copyright 2008, 2009, 2010, 2011, 2017 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Gtk2::Job::Latest;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use Gtk2;
23 use List::Util qw(min max);
24 use Locale::TextDomain ('App-Chart');
25  
26 use App::Chart::Gtk2::Job;
27 use App::Chart::Latest;
28  
29 # uncomment this to run the ### lines
30 # use Smart::Comments;
31  
32 use Glib::Object::Subclass
33 'App::Chart::Gtk2::Job',
34 signals => { notify => \&_do_notify },
35 properties => [ Glib::ParamSpec->boxed
36 ('symbol-list',
37 'Symbol list',
38 'Arrayref of share symbols.',
39 'Glib::Strv',
40 Glib::G_PARAM_READWRITE),
41 ];
42  
43 our %inprogress = ();
44  
45 sub type {
46 return __('Latest');
47 }
48  
49 sub start_symlist {
50 my ($class, $symlist) = @_;
51 ### Job-Latest start_symlist()
52 require App::Chart::Gtk2::Symlist;
53 return $class->start ([$symlist->interested_symbols]);
54 }
55  
56 sub start_for_view {
57 my ($class, $symbol, $symlist) = @_;
58 my $symbol_list = [ $symbol ];
59 my $extra_list = [ ];
60 if ($symlist) {
61 my $l = $symlist->symbol_listref;
62 my @l = List::MoreUtils::after {$_ eq $symbol} @$l;
63 # push @$symbol_list, shift @l;
64 push @$extra_list, splice @l,0,3; # next 4 symbols as extras
65 @l = List::MoreUtils::before {$_ eq $symbol} @$l;
66 if (@l) { push @$extra_list, $l[-1]; } # prev 1 symbol as extra
67 }
68 $class->start ($symbol_list, $extra_list);
69 }
70  
71 sub start {
72 my ($class, $symbol_list, $extra_list) = @_;
73 ### Job-Latest start()
74 ### $symbol_list
75 ### $extra_list
76  
77 my $new_symbol_list = [ grep {defined && !/^\s*$/ && !$inprogress{$_}}
78 @$symbol_list ];
79 if (! @$new_symbol_list) {
80 ### all still inprogress
81 return $inprogress{$symbol_list->[0]};
82 }
83 $symbol_list = $new_symbol_list;
84 $extra_list = [ grep {!$inprogress{$_}} @$extra_list ];
85  
86 my $name = (@$extra_list
87 ? __x('Latest {symbol_list} (and maybe {extra_list})',
88 symbol_list => form_symbol_list($symbol_list),
89 extra_list => form_symbol_list($extra_list))
90 : __x('Latest {symbol_list}',
91 symbol_list => form_symbol_list($symbol_list)));
92  
93 my $job = $class->SUPER::start
94 (args => [ 'latest', $symbol_list, $extra_list ],
95 name => $name,
96 symbol_list => $symbol_list);
97 foreach my $symbol (@$symbol_list) {
98 $inprogress{$symbol} = $job;
99 }
100 ### Job-Latest latest-changed for inprogress: "@$symbol_list"
101 my %symbol_hash;
102 @symbol_hash{@$symbol_list} = ();
103 App::Chart::chart_dirbroadcast()->send_locally
104 ('latest-changed', \%symbol_hash);
105 return $job;
106 }
107  
108 # 'notify' class closure
109 sub _do_notify {
110 my ($self, $pspec) = @_;
111 my $pname = $pspec->get_name;
112 ### Job-Latest notify(): $pname
113 ### value: $self->get($pname)
114  
115 if ($pname eq 'status') {
116 if (! $self->is_stoppable) {
117 my $symbol_list = $self->{'symbol_list'};
118 delete @inprogress{@$symbol_list};
119 ### Job-Latest latest-changed for done: "@$symbol_list"
120  
121 my %symbol_hash;
122 @symbol_hash{@$symbol_list} = (); # hash slice
123 App::Chart::chart_dirbroadcast()->send_locally
124 ('latest-changed', \%symbol_hash);
125 }
126 }
127 return shift->signal_chain_from_overridden(@_);
128 }
129  
130 sub find {
131 my ($class, $symbol) = @_;
132 return $inprogress{$symbol};
133 }
134  
135 sub form_symbol_list {
136 my ($symbol_list) = @_;
137 my @list = @$symbol_list[0 .. min ($#$symbol_list, 4)];
138 if (@list < @$symbol_list) { push @list, __('...'); }
139 return join (' ', @list);
140 }
141  
142 1;
143 __END__