JobLog/Command/resume.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 128 CC 15 SCAR 50.6
CC
15
Cov
0%
execute
240.0
usage_desc
2.0
abstract
2.0
CRAP 157.2
1 package App::JobLog::Command::resume;
2 $App::JobLog::Command::resume::VERSION = '1.042';
3 # ABSTRACT: resume last closed task
4  
5 use App::JobLog -command;
6 use Modern::Perl;
7 use Class::Autouse 'App::JobLog::Log';
8 use autouse 'App::JobLog::Time' => qw(now);
9  
10 sub execute {
11 my ( $self, $opt, $args ) = @_;
12  
13 # construct event test
14 my %must = map { $_ => 1 } @{ $opt->tag || [] };
15 my %mustnt = map { $_ => 1 } @{ $opt->without || [] };
16 my $test = sub {
17 my $event = shift;
18 return if $event->is_note;
19 my @tags = @{ $event->tags };
20 my %tags = map { $_ => 1 } @tags;
21 my $good = 1;
22 if (%must) {
23 if ( $opt->any ) {
24 $good = 0;
25 for my $tag (@tags) {
26 if ( $must{$tag} ) {
27 $good = 1;
28 last;
29 }
30 }
31 }
32 else {
33 for my $tag ( keys %must ) {
34 unless ( $tags{$tag} ) {
35 $good = 0;
36 last;
37 }
38 }
39 }
40 }
41 if ( $good && %mustnt ) {
42 if ( $opt->some ) {
43 $good = 0;
44 for my $tag ( keys %mustnt ) {
45 unless ( $tags{$tag} ) {
46 $good = 1;
47 last;
48 }
49 }
50 }
51 else {
52 for my $tag (@tags) {
53 if ( $mustnt{$tag} ) {
54 $good = 0;
55 last;
56 }
57 }
58 }
59 }
60 return $good;
61 };
62  
63 # find event
64 my $log = App::JobLog::Log->new;
65 my ( $i, $count, $e ) = ( $log->reverse_iterator, 0 );
66 while ( $e = $i->() ) {
67 $count++;
68 last if $test->($e);
69 }
70  
71 $self->usage_error('empty log') unless $count;
72 $self->usage_error('no matching event') unless $e;
73 $self->usage_error('event ongoing') unless $e->is_closed;
74  
75 my $ll = $e->data->clone;
76 $ll->time = now;
77 $log->append_event($ll);
78 print "resuming " . $e->describe;
79 print ' (tags: ', join( ', ', sort $e->tag_list ), ')' if $e->tagged;
80 print "\n";
81 }
82  
83 sub usage_desc { '%c ' . __PACKAGE__->name . ' %o' }
84  
85 sub abstract { 'resume last task' }
86  
87 sub full_description {
88 <<END
89 Starts a new task with an identical description and tags to the last
90 task in the log. If some restriction by tag is specified, it is the last
91 task with the given tags.
92 END
93 }
94  
95 sub options {
96 return (
97 [
98 'tag|t=s@',
99 'resume the last event with all of these tags; '
100 . 'multiple tags may be specified'
101 ],
102 [ 'any|a', 'require only that one of the --tag tags be present' ],
103 [
104 'without|w=s@',
105 'resume the last event which does not have any of these tags; '
106 . 'multiple tags may be specified'
107 ],
108 [
109 'some|s', 'require only that some one of the --without tags be absent'
110 ],
111 );
112 }
113  
114 1;
115  
116 __END__