Timeline/Event.pm

filter: Statementstmt 92.1% 59 / 64 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 75.0% 3 / 4 totaltotal 82.4% 80 / 97 CC 6 SCAR 5.9
CC
6
Cov
87%
draw_on
4.0
end_year
2.1
str2num
2.0
CRAP 1.8
↑ Summary Timeline.pm »
1 =head1 NAME
2  
3 SVG::Timelime::Event - A single event in an SVG timeline.
4  
5 =head1 SYNOPSIS
6  
7 See L<SVG::Timeline>.
8  
9 =cut
10  
11 package SVG::Timeline::Event;
12  
13 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 coerce __PACKAGE__,
21 from 'HashRef',
22 7 ▶ via { __PACKAGE__->new($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 # Chosen format: yyyy-mm-dd
25 subtype 'SVG::Timeline::DateStr',
26 as 'Str',
27 2 ▶ where { m/ \d{4}-\d{2}-\d{2} /ax };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 subtype 'SVG::Timeline::YearMonthStr',
30 as 'Str',
31 0 ▶ where { m/ \d{4}-\d{2} /ax };
Subroutine __ANON__: not called CC=1 SCAR=1.2
32  
33 subtype 'SVG::Timeline::YearStr',
34 as 'Str',
35 0 ▶ where { m/ \d{4} /as };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36  
37 2 subtype 'SVG::Timeline::Num',
38 as 'Num';
39  
40 coerce 'SVG::Timeline::DateStr',
41 from 'SVG::Timeline::YearStr',
42 0 ▶ via { $_ . '-01-01' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
43  
44 coerce 'SVG::Timeline::DateStr',
45 from 'SVG::Timeline::YearMonthStr',
46 0 ▶ via { $_ . '-01' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
47  
48 2 coerce 'SVG::Timeline::Num',
49 from 'SVG::Timeline::DateStr',
50 via \&str2num;
51  
52 2 has index => (
53 is => 'ro',
54 isa => 'Int',
55 required => 1,
56 );
57  
58 2 has text => (
59 is => 'ro',
60 isa => 'Str',
61 required => 1,
62 );
63  
64 sub str2num {
65 2 ▶ my ($datestr) = @_;
Subroutine str2num: called CC=2 SCAR=7.1
Pod undocumented
66  
67 2 my $date = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' )
68 ->parse_datetime($datestr);
69  
70 2 ▶ return $date->year + ( $date->day_of_year / ($date->is_leap_year ? 366 : 365) );
Branch: 1 branch
BranchTrueFalseTotal
$date->is_leap_year ? : 0 2 2
71 }
72  
73 2 has start => (
74 is => 'ro',
75 isa => 'SVG::Timeline::Num',
76 required => 1,
77 coerce => 1,
78 );
79  
80 2 has end => (
81 is => 'ro',
82 isa => 'SVG::Timeline::Num',
83 required => 1,
84 coerce => 1,
85 );
86  
87 2 has colour => (
88 is => 'ro',
89 isa => 'Maybe[Str]',
90 required => 0,
91 );
92  
93 =head1 METHODS
94  
95 =head2 start_year
96  
97 Return the year that the event started.
98  
99 =cut
100  
101 sub start_year {
102 121 ▶ my $self = shift;
Subroutine start_year: called CC=1 SCAR=0.0
Pod documented
103  
104 121 return int $self->start;
105 }
106  
107 =head2 end_year
108  
109 Return the year that the event ended.
110  
111 =cut
112  
113 sub end_year {
114 388 ▶ my $self = shift;
Subroutine end_year: called CC=2 SCAR=7.6
Pod documented
115  
116 388 ▶ if ($self->end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end) { } 388 0 388
117 388 return int $self->end;
118 } else {
119 0 return DateTime->now->year;
120 }
121 }
122  
123 =head2 draw_on($tl)
124  
125 Draw the event inside the given timeline object.
126  
127 =cut
128  
129 sub draw_on {
130 7 ▶ my $self = shift;
Subroutine draw_on: called CC=4 SCAR=13.9
Pod documented
131 7 my ($tl) = @_;
132  
133 7 my $x = $self->start * $tl->units_per_year;
134 7 my $y = ($tl->bar_height * $self->index)
135 + ($tl->bar_height * $tl->bar_spacing
136 * ($self->index - 1));
137  
138 7 my $width = ($self->end - $self->start) * $tl->units_per_year;
139  
140 7 ▶ $tl->rect(
Condition33%1/3
$self->colour // $tl->default_colour
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->colour // $tl->default_colour
$self->colour $tl->default_colour
Truth table33%1/3
A // B
A= $self->colourB= $tl->default_colour
ABresult
1X1
011
000
141 x => $x,
142 y => $y,
143 width => $width,
144 height => $tl->bar_height,
145 fill => $self->colour // $tl->default_colour,
146 stroke => $tl->bar_outline_colour,
147 'stroke-width' => 1
148 );
149  
150 7 my $font_size = $tl->bar_height * 0.8;
151 7 my $padding = $tl->bar_height * 0.2;
152 7 my $text_x = $x + $padding;
153 7 my $text_anchor = 'start';
154  
155 # Estimate text width: approximate average character width is 0.6 × font-size
156 7 my $approx_text_width = length($self->text) * $font_size * 0.6;
157 7 my $right_edge = $x + $width;
158 7 my $image_right_edge = $tl->max_year * $tl->units_per_year;
159  
160 7 ▶ if ($text_x + $approx_text_width > $image_right_edge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_x + $approx_text_width > $image_right_edge) 2 5 7
161 2 ▶ $text_x = ($right_edge < $image_right_edge ? $right_edge : $image_right_edge) - $padding;
Branch: 1 branch
BranchTrueFalseTotal
$right_edge < $image_right_edge ? : 0 2 2
162 2 $text_anchor = 'end';
163 }
164  
165 $tl->text(
166 7 x => $text_x,
167 y => $y + $font_size,
168 'font-size' => $font_size,
169 'text-anchor' => $text_anchor,
170 )->cdata($self->text);
171 }
172  
173 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 2 __PACKAGE__->meta->make_immutable;
175  
176 =head1 AUTHOR
177  
178 Dave Cross <dave@perlhacks.com>
179  
180 =head1 COPYRIGHT AND LICENCE
181  
182 Copyright (c) 2017, Magnum Solutions Ltd. All Rights Reserved.
183  
184 This library is free software; you can redistribute it and/or modify it
185 under the same terms as Perl itself.
186  
187 =cut
188  
189 2 1;
↑ Summary Timeline.pm »