Jiffy.pm

filter: Statementstmt 29.4% 40 / 136 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 12 / 21 Podpod 83.3% 5 / 6 totaltotal 26.7% 57 / 213 CC 24 SCAR 31.4
CC
24
Cov
22%
add_entry
56.0
search
42.0
time_sheet
30.0
CRAP 23.2
« Jiffy/View/Timesheet.pm ↑ Summary Jiffy/Util/Duration.pm »
1 package App::Jiffy;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use open ':std', ':encoding(utf8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our $VERSION = '0.09';
11  
12 1 ▶use App::Jiffy::TimeEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use App::Jiffy::View::Timesheet;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use App::Jiffy::Util::Duration qw/round/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use YAML::Any qw( LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use JSON::MaybeXS 'JSON';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 Getopt::Long::Configure("pass_through");
21  
22 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has cfg => (
25 is => 'ro',
26 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
27 0 ▶ LoadFile( $ENV{HOME} . '/.jiffy.yml' ) || {};
Branch: 1 branch
BranchTrueFalseTotal
LoadFile($ENV{"HOME"} . '/.jiffy.yml') or {} 0 0 0
28 },
29 1 );
30  
31 has terminator_regex => (
32 is => 'ro',
33 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
34 0 ▶ die 'terminator_regex must be a regex ref' unless ref $_[0] eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "Regexp" 0 0 0
35 },
36 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 qr/^end$|
38 ^done$|
39 ^eod$|
40 ^finished$|
41 ^\\\(^\s*\.^\s*\)\/$| # This is a smily face with hands raised
42 ^✓$|
43 ^x$/x;
44 },
45 1 );
46  
47 0 ▶sub remove_terminators {
Subroutine remove_terminators: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $self = shift;
49 return (
50 0 title => {
51 '$not' => $self->terminator_regex,
52 } );
53 }
54  
55 0 ▶sub add_entry {
Subroutine add_entry: not called CC=7 SCAR=40.3
Pod documented
56 0 my $self = shift;
57 0 my $options = shift;
58 0 my $title;
59 0 ▶ if ( ref $options ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options ne 'HASH') { } 0 0 0
60 0 $title = $options;
61 0 undef $options;
62 } else {
63 0 $title = shift;
64 }
65  
66 0 my $start_time;
67  
68 0 my $LocalTZ = DateTime::TimeZone->new( name => 'local' ); # For caching
69 0 my $now = DateTime->now( time_zone => $LocalTZ );
70  
71 0 ▶ if ( $options->{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"time"}) 0 0 0
72 0 require DateTime::Format::Strptime;
73  
74 # @TODO Figure out something more flexible and powerful to get time
75  
76 # First try H:M:S
77 0 my $strp = DateTime::Format::Strptime->new(
78 pattern => '%T',
79 time_zone => $LocalTZ,
80 );
81 0 $start_time = $strp->parse_datetime( $options->{time} );
82  
83 # If no time found try just H:M
84 0 ▶ if ( not $start_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start_time) 0 0 0
85 0 my $strp = DateTime::Format::Strptime->new(
86 pattern => '%R',
87 time_zone => $LocalTZ,
88 );
89 0 $start_time = $strp->parse_datetime( $options->{time} );
90 }
91  
92 # Make sure the date part of the datetime is not set to the
93 # beginning of time.
94 0 ▶ if ( $start_time and $start_time->year < $now->year ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_time and $start_time->year < $now->year) 0 0 0
Condition0%0/3
$start_time and $start_time->year < $now->year
!ll&&!rl&&r
000
MC/DC0%0/2
$start_time and $start_time->year < $now->year
$start_time $start_time->year < $now->year
Truth table0%0/3
A and B
A= $start_timeB= $start_time->year < $now->year
ABresult
0X0
100
111
95 0 $start_time->set(
96 day => $now->day,
97 month => $now->month,
98 year => $now->year,
99 );
100 }
101 }
102  
103 # Create and save Entry
104 App::Jiffy::TimeEntry->new(
105 0 ▶ title => $title,
Condition0%0/3
$start_time // $now
l!l&&r!l&&!r
000
MC/DC0%0/2
$start_time // $now
$start_time $now
Truth table0%0/3
A // B
A= $start_timeB= $now
ABresult
1X1
011
000
106 start_time => $start_time // $now,
107 cfg => $self->cfg,
108 )->save;
109 }
110  
111 0 ▶sub current_time {
Subroutine current_time: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113  
114 0 my $latest = App::Jiffy::TimeEntry::last_entry( $self->cfg );
115 0 my $duration = $latest->duration;
116  
117 0 print '"' . $latest->title . '" has been running for';
118  
119 0 my %deltas = $duration->deltas;
120 0 foreach my $unit ( keys %deltas ) {
121 0 ▶ next unless $deltas{$unit};
Branch: 1 branch
BranchTrueFalseTotal
unless $deltas{$unit} 0 0 0
122 0 print " " . $deltas{$unit} . " " . $unit;
123 }
124 0 print ".\n";
125 }
126  
127 0 ▶sub time_sheet {
Subroutine time_sheet: not called CC=5 SCAR=34.0
Pod documented
128 0 my $self = shift;
129 0 my $options = shift;
130 0 my $from;
131 0 ▶ if ( ref $options ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options ne 'HASH') { } 0 0 0
132 0 $from = $options;
133 0 undef $options;
134 } else {
135 0 $from = shift;
136 }
137  
138 0 my $from_date = DateTime->today( time_zone => 'local' );
139  
140 0 ▶ if ( defined $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 0 0 0
141 0 $from_date->subtract( days => $from );
142 }
143  
144 0 my @entries = App::Jiffy::TimeEntry::search(
145 $self->cfg,
146 query => {
147 start_time => { '$gt' => $from_date, },
148 $self->remove_terminators,
149 },
150 sort => {
151 start_time => 1,
152 },
153 );
154  
155 0 ▶ if ( $options->{round} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"round"}) 0 0 0
156 0 @entries = map { $_->duration( round( $_->duration ) ); $_ } @entries;
157 }
158  
159 0 ▶ if ( $options->{json} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'json'}) { } 0 0 0
160 0 my $json = JSON::MaybeXS->new( pretty => 1, convert_blessed => 1 );
161 0 print $json->encode( \@entries );
162 } else {
163 0 $options->{from} = $from;
164 0 App::Jiffy::View::Timesheet::render( \@entries, $options );
165 }
166 }
167  
168 0 ▶sub search {
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
169 0 my $self = shift;
170 0 my $query_text = shift;
171 0 my $options = shift;
172 0 my $days;
173 0 ▶ if ( ref $options ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options ne 'HASH') { } 0 0 0
174 0 $days = $options;
175 0 undef $options;
176 } else {
177 0 $days = shift;
178 }
179  
180 0 my $from_date = DateTime->today( time_zone => 'local' );
181  
182 0 ▶ if ( defined $days ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $days) 0 0 0
183 0 $from_date->subtract( days => $days );
184 }
185  
186 0 my @entries = App::Jiffy::TimeEntry::search(
187 $self->cfg,
188 query => {
189 start_time => { '$gt' => $from_date, },
190 $self->remove_terminators,
191 title => qr/$query_text/,
192 },
193 sort => {
194 start_time => 1,
195 },
196 );
197  
198 0 ▶ if ( $options->{round} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"round"}) 0 0 0
199 0 @entries = map { $_->duration( round( $_->duration ) ); $_ } @entries;
200 }
201  
202 0 ▶ if ( not @entries ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@entries) 0 0 0
203 0 print "No Entries Found\n";
204 0 return;
205 }
206  
207 0 ▶ if ( $options->{json} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'json'}) { } 0 0 0
208 0 my $json = JSON::MaybeXS->new( pretty => 1, convert_blessed => 1 );
209 0 print $json->encode( \@entries );
210 } else {
211 0 $options->{from} = $days;
212 0 App::Jiffy::View::Timesheet::render( \@entries, $options );
213 }
214 }
215  
216 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
217 0 my $self = shift;
218 0 my @args = @_;
219  
220 0 ▶ if ( $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]) 0 0 0
221 0 ▶ if ( $args[0] eq 'current' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args[0] eq 'current') { } 0 0 0
elsif ($args[0] eq 'timesheet') { } 0 0 0
elsif ($args[0] eq 'search') { } 0 0 0
222 0 shift @args;
223 0 return $self->current_time(@args);
224 } elsif ( $args[0] eq 'timesheet' ) {
225 0 shift @args;
226  
227 0 my $p = Getopt::Long::Parser->new( config => ['pass_through'], );
228 0 $p->getoptionsfromarray(
229 \@args,
230 'verbose' => \my $verbose,
231 'round' => \my $round,
232 'json' => \my $json
233 );
234  
235 0 return $self->time_sheet( {
236 verbose => $verbose,
237 round => $round,
238 json => $json,
239 },
240 @args
241 );
242 } elsif ( $args[0] eq 'search' ) {
243 0 shift @args;
244  
245 0 my $p = Getopt::Long::Parser->new( config => ['pass_through'], );
246 0 $p->getoptionsfromarray(
247 \@args,
248 'verbose' => \my $verbose,
249 'round' => \my $round,
250 'json' => \my $json
251 );
252  
253 0 my $query_text = shift @args;
254  
255 0 return $self->search(
256 $query_text,
257 {
258 verbose => $verbose,
259 round => $round,
260 json => $json,
261 },
262 @args
263 );
264 }
265 }
266  
267 0 my $p = Getopt::Long::Parser->new( config => ['pass_through'], );
268 0 $p->getoptionsfromarray( \@args, 'time=s' => \my $time, );
269  
270 0 return $self->add_entry( {
271 time => $time,
272 },
273 join ' ',
274 @args
275 );
276 }
277  
278 1 1;
279 __END__
« Jiffy/View/Timesheet.pm ↑ Summary Jiffy/Util/Duration.pm »