JobLog/Vacation/Period.pm untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 379 CC 34 SCAR 34.5
CC
34
Cov
0%
cmp
72.0
conflicts
72.0
_properties
56.0
CRAP 31.6
1 package App::JobLog::Vacation::Period;
2 $App::JobLog::Vacation::Period::VERSION = '1.042';
3 # ABSTRACT: extension of L<App::JobLog::Log::Event> to handle special properties of vacation periods
4  
5  
6 use Exporter 'import';
7 our @EXPORT_OK = qw(
8 FLEX
9 FIXED
10 ANNUAL
11 MONTHLY
12 );
13  
14 use base 'App::JobLog::Log::Event';
15 use DateTime;
16 use App::JobLog::Log::Line;
17 use App::JobLog::Time qw(tz);
18 use Carp qw(carp);
19  
20 use overload '""' => \&to_string;
21 use overload 'bool' => sub { 1 };
22  
23 use constant FLEX => 1;
24 use constant FIXED => 2;
25 use constant ANNUAL => 1;
26 use constant MONTHLY => 2;
27  
28 sub new {
29 my ( $class, $log_line, %opts ) = @_;
30 $class = ref $class || $class;
31 bless {
32 log => $log_line,
33 type => 0,
34 repeats => 0,
35 tags => [],
36 events => [],
37 vacation => [],
38 %opts
39 },
40 $class;
41 }
42  
43  
44 sub flex { $_[0]->{type} == FLEX }
45  
46  
47 sub fixed { $_[0]->{type} == FIXED }
48  
49  
50 sub annual { $_[0]->{repeats} == ANNUAL }
51  
52  
53 sub monthly { $_[0]->{repeats} == MONTHLY }
54  
55  
56 sub repeats { $_[0]->{repeats} }
57  
58  
59 sub description : lvalue {
60 $_[0]->data->description;
61 }
62  
63  
64 sub clone {
65 my ($self) = @_;
66 my $clone = $self->SUPER::clone;
67 $clone->{type} = $self->{type};
68 $clone->{repeats} = $self->{repeats};
69 return $clone;
70 }
71  
72  
73 sub cmp {
74 my ( $self, $other ) = @_;
75  
76 # when mixed with ordinary events
77 if ( ref $other eq 'App::JobLog::Log::Event' ) {
78  
79 # treat as an ordinary event if fixed
80 return $self->SUPER::cmp($other) if $self->fixed;
81  
82 # put after ordinary events
83 return 1;
84 }
85 if ( $self->monthly ) {
86 return -1 unless $other->monthly;
87 }
88 elsif ( $self->annual ) {
89 return 1 if $other->monthly;
90 return -1 unless $other->annual;
91 }
92 return $self->SUPER::cmp($other);
93 }
94  
95 # some global variables for use in BNF regex
96 our ( @dates, $type, @tags, $description );
97  
98 # log line parser
99 my $re = qr{
100 ^ (?&ts) : (?&non_ts) $
101 (?(DEFINE)
102 (?<ts> (?&date) : (?&date) )
103 (?<date> (\d{4}\s++\d++\s++\d++\s++\d++\s++\d++\s++\d++) (?{push @dates, $^N}) )
104 (?<non_ts> (?&flex) : (?&tags) : (?&description))
105 (?<flex> ([012]{2}) (?{$type = $^N}))
106 (?<tags> (?:(?&tag)(\s++(?&tag))*+)?)
107 (?<tag> ((?:[^\s:\\]|(?&escaped))++) (?{push @tags, $^N}))
108 (?<escaped> \\.)
109 (?<description> (.++) (?{$description = $^N}))
110 )
111 }xi;
112  
113  
114 sub parse {
115 my ( $class, $text ) = @_;
116 $class = ref $class || $class;
117 local ( @dates, $type, @tags, $description );
118 if ( $text =~ $re ) {
119 my $start = _parse_time( $dates[0] );
120 my $end = _parse_time( $dates[1] );
121 my %tags = map { $_ => 1 } @tags;
122 my $tags = [ map { s/\\(.)/$1/g; $_ } sort keys %tags ];
123 $description = [ map { s/\\(.)/$1/g; $_ } ($description) ];
124 my ( $type, $repeats ) = split //, $type;
125 $obj = $class->new(
126 App::JobLog::Log::Line->new(
127 description => $description,
128 time => $start,
129 tags => $tags
130 ),
131 type => $type,
132 repeats => $repeats,
133 end => $end
134 );
135 return $obj;
136 }
137 else {
138 carp "malformed line in vacation file: '$text'";
139 }
140 return;
141 }
142  
143 sub _parse_time {
144 my @time = split /\s++/, $_[0];
145 $date = DateTime->new(
146 year => $time[0],
147 month => $time[1],
148 day => $time[2],
149 hour => $time[3],
150 minute => $time[4],
151 second => $time[5],
152 time_zone => tz,
153 );
154 return $date;
155 }
156  
157  
158 sub to_string {
159 my ($self) = @_;
160 my $text = $self->data->time_stamp( $self->start );
161 $text .= ':';
162 $text .= $self->data->time_stamp( $self->end );
163 $text .= ':';
164 if ( $self->flex ) {
165 $text .= FLEX;
166 }
167 elsif ( $self->fixed ) {
168 $text .= FIXED;
169 }
170 else {
171 $text .= 0;
172 }
173 if ( $self->annual ) {
174 $text .= ANNUAL;
175 }
176 elsif ( $self->monthly ) {
177 $text .= MONTHLY;
178 }
179 else {
180 $text .= 0;
181 }
182 $text .= ':';
183 $self->tags ||= [];
184 my %tags = map { $_ => 1 } @{ $self->tags };
185 $text .= join ' ', map { s/([:\\\s])/\\$1/g; $_ } sort keys %tags;
186 $text .= ':';
187 $self->description ||= [];
188 $text .= join ';',
189 map { ( my $d = $_ ) =~ s/([;\\])/\\$1/g; $d } @{ $self->description };
190 }
191  
192  
193 sub conflicts {
194 my ( $self, $other ) = @_;
195 return 1 if $self->intersects($other);
196 my $other_is_period = ref $other eq __PACKAGE__;
197 if ( $self->annual || $other_is_period && $other->annual ) {
198 if ( $self->start->year != $other->start->year ) {
199 if ( !$self->annual ) {
200 my $t = $self;
201 $self = $other;
202 $other = $t;
203 }
204 $self = $self->clone;
205 my $d = $self->start->year - $other->start->year;
206 $self->start->subtract( years => $d );
207 $self->end->subtract( years => $d );
208 return $self->intersects($other);
209 }
210 }
211 elsif ( $self->monthly || $other_is_period && $other->monthly ) {
212 if ( $self->start->year != $other->start->year
213 || $self->start->month != $other->start->month )
214 {
215 if ( !$self->monthly ) {
216 my $t = $self;
217 $self = $other;
218 $other = $t;
219 }
220 $self = $self->clone;
221 my $d = $self->start->year - $other->start->year;
222 $self->start->subtract( years => $d );
223 $self->end->subtract( years => $d );
224 $d = $self->start->month - $other->start->month;
225 $self->start->subtract( months => $d );
226 $self->end->subtract( months => $d );
227 return $self->intersects($other);
228 }
229 }
230 return 0;
231 }
232  
233  
234 sub parts {
235 my ($self) = @_;
236 return $self->_time, $self->_properties, $self->_tags, $self->_description;
237 }
238  
239  
240 sub single_day {
241 my ($self) = @_;
242 my ( $s, $e ) = ( $self->start, $self->end );
243 return $s->year == $e->year && $s->month == $e->month && $s->day == $e->day;
244 }
245  
246 # time part of summary
247 sub _time {
248 my ($self) = @_;
249 my $fmt;
250 if ( $self->annual ) {
251 $fmt = '%b %d';
252 }
253 elsif ( $self->monthly ) {
254 $fmt = '%d';
255 }
256 else {
257 $fmt = '%F';
258 }
259 $fmt .= ' %H:%M:%S' if $self->fixed;
260 my $s;
261 if ( $self->single_day ) {
262 $s = $self->start->strftime($fmt);
263 }
264 else {
265 $s = $self->start->strftime($fmt) . ' -- ' . $self->end->strftime($fmt);
266 }
267 return $s;
268 }
269  
270 # properties part of summary
271 sub _properties {
272 my ($self) = @_;
273 my $s;
274 if ( $self->fixed ) {
275 $s = 'fixed';
276 }
277 elsif ( $self->flex ) {
278 $s = 'flex';
279 }
280 else {
281 $s = '';
282 }
283 if ( $self->annual ) {
284 $s .= ' ' if $s;
285 $s .= 'annual';
286 }
287 elsif ( $self->monthly ) {
288 $s .= ' ' if $s;
289 $s .= 'monthly';
290 }
291 return $s;
292 }
293  
294  
295 sub overlap {
296 my ( $self, $start, $end ) = @_;
297 if ( $self->annual || $self->monthly ) {
298  
299 # cloning here should be duplicated work, but better safe than sorry
300 my $cloned = 0;
301 if ( $self->annual
302 || $self->monthly && $self->start->year != $start->year )
303 {
304 $self = $self->clone;
305 $cloned = 1;
306 my $delta = $start->year - $self->start->year;
307 $self->start->add( years => $delta );
308 $self->end->add( years => $delta );
309 }
310 if ( $self->monthly && $self->start->month != $start->month ) {
311 $self = $self->clone unless $cloned;
312 my $delta = $start->month - $self->start->month;
313 $self->start->add( months => $delta );
314 $self->end->add( months => $delta );
315 }
316 }
317 return $self->SUPER::overlap( $start, $end );
318 }
319  
320 # tag part of summary
321 sub _tags {
322 my ($self) = @_;
323 return join ', ', @{ $self->tags };
324 }
325  
326 # description part of summary
327 sub _description {
328 my ($self) = @_;
329 return join '; ', @{ $self->description };
330 }
331  
332 1;
333  
334 __END__