JobLog/Command/last.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 116 CC 14 SCAR 49.9
CC
14
Cov
0%
execute
210.0
usage_desc
2.0
abstract
2.0
CRAP 147.6
1 package App::JobLog::Command::last;
2 $App::JobLog::Command::last::VERSION = '1.042';
3 # ABSTRACT: show details of last recorded event
4  
5 use Modern::Perl;
6 use App::JobLog -command;
7 use Class::Autouse qw(
8 App::JobLog::Log
9 App::JobLog::Command::summary
10 );
11  
12 sub execute {
13 my ( $self, $opt, $args ) = @_;
14  
15 # construct event test
16 my %must = map { $_ => 1 } @{ $opt->tag || [] };
17 my %mustnt = map { $_ => 1 } @{ $opt->without || [] };
18 my $test = sub {
19 my $event = shift;
20 return if $event->is_note;
21 my @tags = @{ $event->tags };
22 my %tags = map { $_ => 1 } @tags;
23 my $good = 1;
24 if (%must) {
25 if ( $opt->any ) {
26 $good = 0;
27 for my $tag (@tags) {
28 if ( $must{$tag} ) {
29 $good = 1;
30 last;
31 }
32 }
33 }
34 else {
35 for my $tag ( keys %must ) {
36 unless ( $tags{$tag} ) {
37 $good = 0;
38 last;
39 }
40 }
41 }
42 }
43 if ( $good && %mustnt ) {
44 if ( $opt->some ) {
45 $good = 0;
46 for my $tag ( keys %mustnt ) {
47 unless ( $tags{$tag} ) {
48 $good = 1;
49 last;
50 }
51 }
52 }
53 else {
54 for my $tag (@tags) {
55 if ( $mustnt{$tag} ) {
56 $good = 0;
57 last;
58 }
59 }
60 }
61 }
62 return $good;
63 };
64  
65 # find event
66 my ( $i, $count, $e ) = ( App::JobLog::Log->new->reverse_iterator, 0 );
67 while ( $e = $i->() ) {
68 $count++;
69 last if $test->($e);
70 }
71  
72 if ($e) {
73 my $start = $e->start->strftime('%F at %H:%M:%S %p');
74 my $end = $e->is_open ? 'now' : $e->end->strftime('%F at %H:%M:%S %p');
75 $opt->{merge} = 'no_merge';
76 App::JobLog::Command::summary->execute( $opt, ["$start - $end"] );
77 }
78 else {
79 say $count ? 'no matching event' : 'empty log';
80 }
81 }
82  
83 sub usage_desc { '%c ' . __PACKAGE__->name }
84  
85 sub abstract { 'describe the last task recorded' }
86  
87 sub options {
88 return (
89 [
90 'tag|t=s@',
91 'find the last event with all of these tags; '
92 . 'multiple tags may be specified'
93 ],
94 [ 'any|a', 'require only that one of the --tag tags be present' ],
95 [
96 'without|w=s@',
97 'find the last event which does not have any of these tags; '
98 . 'multiple tags may be specified'
99 ],
100 [
101 'some|s', 'require only that some one of the --without tags be absent'
102 ],
103 );
104 }
105  
106 1;
107  
108 __END__