JobLog/Log/Event.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 157 CC 14 SCAR 28.2
CC
14
Cov
0%
cmp
42.0
overlap
30.0
clone
6.0
CRAP 16.7
1 package App::JobLog::Log::Event;
2 $App::JobLog::Log::Event::VERSION = '1.042';
3 # ABSTRACT: basically adds an end time to App::JobLog::Log::Line events
4  
5  
6 use parent qw(App::JobLog::Log::Note);
7  
8 use Modern::Perl;
9 use Class::Autouse qw{DateTime};
10 use autouse 'App::JobLog::Time' => qw(now);
11 use autouse 'Carp' => qw(carp);
12  
13 # for debugging
14 use overload '""' => sub {
15 $_[0]->data->to_string . '-->'
16 . ( $_[0]->is_closed ? $_[0]->end : 'ongoing' );
17 };
18  
19  
20 sub clone {
21 my ($self) = @_;
22 my $clone = $self->new( $self->data->clone );
23 $clone->end = $self->end->clone unless $self->is_open;
24 return $clone;
25 }
26  
27  
28 sub overlap {
29 my ( $self, $start, $end ) = @_;
30  
31 # if this falls entirely within interval, return this
32 my $c1 = DateTime->compare( $start, $self->start ) || 0;
33 my $c2 = DateTime->compare( $end, $self->end ) || 0;
34 if ( $c1 <= 0 && $c2 >= 0 ) {
35 return $self;
36 }
37 return if $self->start >= $end || $start >= $self->end;
38 my $s = $c1 < 0 ? $self->start : $start;
39 my $e = $c2 < 0 ? $end : $self->end;
40 my $clone = $self->clone;
41 $clone->start = $s;
42 $clone->end = $e;
43 return $clone;
44 }
45  
46  
47 sub end : lvalue {
48 $_[0]->{end};
49 }
50  
51  
52 sub cmp {
53 my ( $self, $other ) = @_;
54 my $comparison = $self->SUPER::cmp($other);
55 unless ($comparison) {
56 if ( $other->isa(__PACKAGE__) ) {
57 if ( $self->is_closed ) {
58 if ( $other->is_closed ) {
59 return DateTime->compare( $self->end, $other->end );
60 }
61 else {
62 return 1;
63 }
64 }
65 elsif ( $other->is_closed ) {
66 return -1;
67 }
68 else {
69 return 0;
70 }
71 }
72 }
73 return $comparison;
74 }
75  
76  
77 sub is_closed { $_[0]->{end} }
78  
79  
80 sub is_open { !$_[0]->is_closed }
81  
82  
83 sub duration {
84 my ($self) = @_;
85 my $e = $self->is_open ? now : $self->end;
86 return $e->epoch - $self->start->epoch;
87 }
88  
89  
90 sub split_days {
91 my ($self) = @_;
92 my $days_end =
93 $self->start->clone->truncate( to => 'day' )->add( days => 1 );
94 my $e = $self->end || now;
95 if ( $days_end < $e ) {
96 my @splits;
97 my $s = $self->start;
98 do {
99 my $clone = $self->clone;
100 $clone->start = $s;
101 $s = $days_end->clone;
102 $clone->end = $s;
103 push @splits, $clone;
104 $days_end->add( days => 1 );
105 } while ( $days_end < $e );
106 my $clone = $self->clone;
107 $clone->start = $s;
108 $clone->end = $self->end;
109 push @splits, $clone;
110 return @splits;
111 }
112 else {
113 return $self;
114 }
115 }
116  
117  
118 sub intersects {
119 my ( $self, $other ) = @_;
120 if ( $self->start > $other->start ) {
121  
122 #rearrange so $self is earlier
123 my $t = $other;
124 $other = $self;
125 $self = $t;
126 }
127 return $self->is_open || $self->end > $other->start;
128 }
129  
130 1;
131  
132 __END__