YamlTime/Task.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 212 CC 21 SCAR 39.7
CC
21
Cov
0%
check
156.0
remove
20.0
BUILD
12.0
CRAP 52.8
1 ##
2 # name: YamlTime::Task
3 # abstract: YamlTime Task Object Class
4 # author: Ingy döt Net <ingy@cpan.org>
5 # license: perl
6 # copyright: 2011
7 # see:
8 # - YamlTime
9  
10 #-----------------------------------------------------------------------------#
11 package YamlTime::Task;
12 use Mouse;
13 use YAML::XS;
14 use Template::Toolkit::Simple;
15 use Template::Toolkit::Simple;
16 use IO::All;
17 # use XXX;
18  
19 sub BUILD {
20 my $self = shift;
21 $self->{id} ||= $self->new_id;
22 my $id = $self->id;
23 die "'$id' is invalid task id"
24 unless $id =~ m!^20\d\d/\d\d/\d\d/\d\d\d\d$!;
25 $self->read if $self->exists;
26 }
27  
28 sub conf { $YamlTime::Conf };
29  
30 has id => ( is => 'ro', required => 1 );
31 has mark => ( is => 'rw', default => '' );
32 has time => ( is => 'rw', default => '0:00' );
33 has task => ( is => 'rw', default => '' );
34 has cust => ( is => 'rw', default => '' );
35 has proj => ( is => 'rw', default => '' );
36 has tags => ( is => 'rw', default => sub{[]});
37 has refs => ( is => 'rw', default => sub{[]});
38 has note => ( is => 'rw', default => '' );
39  
40 sub read {
41 my ($self) = @_;
42 my $id = $self->id;
43 my $hash = eval { YAML::XS::LoadFile($id) }
44 or return;
45 $self->{$_} = $hash->{$_}
46 for keys %$hash;
47 }
48  
49 sub write {
50 my ($self) = @_;
51 my $template_file = YamlTime::Command->share . '/task.yaml.tt';
52 my $template = io($template_file)->all;
53 my $yaml = tt->render(\$template, +{ %$self });
54 io($self->id)->assert->print($yaml);
55 }
56  
57 sub start {
58 my ($self) = @_;
59 my $mark = sprintf "%02d:%02d",
60 $self->conf->now->hour,
61 $self->conf->now->minute;
62 $self->mark($mark);
63 $self->write;
64 $self->current;
65 }
66  
67 sub stop {
68 my ($self) = @_;
69 die unless $self->in_progress;
70 my $time = $self->elapsed;
71 $self->time($time);
72 $self->mark('');
73 $self->write();
74 }
75  
76 sub remove {
77 my ($self) = @_;
78 die if $self->in_progress;
79 die unless $self->exists;
80 unlink($self->id) or die;
81 unlink('_')
82 if readlink('_') eq $self->id;
83 }
84  
85 sub elapsed {
86 my ($self) = @_;
87  
88 my $stop = DateTime->now->set_time_zone($self->conf->timezone);
89 my $start = DateTime->now->set_time_zone($self->conf->timezone);
90  
91 my $mark = $self->mark;
92 if ($mark) {
93 $mark =~ /^(\d+):(\d+)$/ or die;
94 my ($hour, $minute) = ($1, $2);
95 $start->set_hour($hour);
96 $start->set_minute($minute);
97 }
98  
99 my $seconds = ($stop->subtract_datetime_absolute($start))->seconds;
100 my $hours = int($seconds / 3600);
101 $seconds %= 3600;
102 my $minutes = int($seconds / 60);
103 $self->time =~ /^(\d+):(\d+)$/ or die;
104 $hours += $1;
105 $minutes += $2;
106 $hours += int($minutes / 60);
107 $minutes %= 60;
108  
109 my $time = sprintf "%d:%02d", $hours, $minutes;
110 return $time;
111 }
112  
113 sub delete {
114 my ($self) = @_;
115 my $id = $self->id;
116 if (-e '_' and readlink('_') eq $id) {
117 unlink('_');
118 }
119 unlink $id;
120 }
121  
122  
123 sub current {
124 my ($self) = @_;
125 my $id = $self->id;
126 die "'$id' does not exist" unless $self->exists;
127 unlink('_');
128 symlink($id, '_');
129 }
130  
131 sub exists {
132 my ($self) = @_;
133 my $id = $self->id or return;
134 return -e $id;
135 }
136  
137 sub in_progress {
138 my ($self) = @_;
139 return !! $self->mark;
140 }
141  
142 sub new_id {
143 my ($self) = @_;
144 my $now = $self->conf->now;
145 return sprintf "%4d/%02d/%02d/%02d%02d",
146 $now->year,
147 $now->month,
148 $now->day,
149 $now->hour,
150 $now->minute;
151 }
152  
153 sub check {
154 my ($self) = @_;
155 my @errors;
156 eval { YAML::XS::LoadFile $self->id; 1 } or return ($@);
157 if (not $self->mark) {
158 if (not $self->time) {
159 push @errors, "The 'time' field is missing";
160 }
161 elsif ($self->time =~ /^0?0:00$/) {
162 push @errors, "The 'time' field is zero";
163 }
164 elsif ($self->time !~ /^\d?\d:\d\d$/) {
165 push @errors, "The 'time' field is invalid";
166 }
167 }
168 else {
169 if ($self->time !~ /^\d?\d:\d\d$/) {
170 push @errors, "The 'time' field is invalid";
171 }
172 }
173 if (not length $self->task) {
174 push @errors, "The 'task' field is missing";
175 }
176 if (not $self->cust) {
177 push @errors, "The 'cust' field is missing";
178 }
179 elsif (not $self->conf->{cust}->{$self->cust}) {
180 push @errors, sprintf "Invalid 'cust' field '%s'", $self->cust;
181 }
182 else {
183 if (not $self->proj) {
184 push @errors, "The 'proj' field is missing";
185 }
186 elsif (not $self->conf->{proj}->{$self->cust}->{$self->proj}) {
187 push @errors, sprintf "Invalid 'proj' field '%s'", $self->proj;
188 }
189 }
190 for my $tag (@{$self->tags}) {
191 if (not $self->conf->{tags}->{$tag}) {
192 push @errors, sprintf "Invalid 'tags' field '%s'", $tag;
193 }
194 }
195 return @errors;
196 }
197  
198 1;