DBRIS/Formation.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 185 CC 3 SCAR 12.5
CC
3
Cov
0%
parse_carriages
12.0
new
2.0
merge_group_attr
2.0
CRAP 3.5
1 package Travel::Status::DE::DBRIS::Formation;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6  
7 use List::Util qw(uniq);
8  
9 use parent 'Class::Accessor';
10  
11 use Travel::Status::DE::DBRIS::Formation::Group;
12 use Travel::Status::DE::DBRIS::Formation::Sector;
13 use Travel::Status::DE::DBRIS::Formation::Carriage;
14  
15 our $VERSION = '0.40';
16  
17 Travel::Status::DE::DBRIS::Formation->mk_ro_accessors(
18 qw(direction platform train_type));
19  
20 sub new {
21 my ( $obj, %opt ) = @_;
22  
23 my $json = $opt{json};
24  
25 my $ref = {
26 json => $opt{json},
27 train_type => $opt{train_type},
28 platform => $json->{departurePlatform},
29 platform_sched => $json->{departurePlatformSchedule},
30 };
31  
32 bless( $ref, $obj );
33  
34 $ref->parse_carriages;
35 $ref->{destinations} = $ref->merge_group_attr('destination');
36 $ref->{train_numbers} = $ref->merge_group_attr('train_no');
37 $ref->{trains} = $ref->merge_group_attr('train');
38  
39 return $ref;
40 }
41  
42 sub merge_group_attr {
43 my ( $self, $attr ) = @_;
44  
45 my @attrs;
46 my %attr_to_group;
47 my %attr_to_sectors;
48  
49 for my $group ( $self->groups ) {
50 push( @attrs, $group->{$attr} );
51 push( @{ $attr_to_group{ $group->{$attr} } }, $group );
52 push( @{ $attr_to_sectors{ $group->{$attr} } }, $group->sectors );
53 }
54  
55 @attrs = uniq @attrs;
56  
57 return [
58 map {
59 {
60 name => $_,
61 groups => $attr_to_group{$_},
62 sectors => $attr_to_sectors{$_}
63 }
64 } @attrs
65 ];
66 }
67  
68 sub parse_carriages {
69 my ($self) = @_;
70  
71 my $platform_length
72 = $self->{json}{platform}{end} - $self->{json}{platform}{start};
73  
74 for my $sector ( @{ $self->{json}{platform}{sectors} } ) {
75 push(
76 @{ $self->{sectors} },
77 Travel::Status::DE::DBRIS::Formation::Sector->new(
78 json => $sector,
79 platform => {
80 start => $self->{json}{platform}{start},
81 end => $self->{json}{platform}{end},
82 }
83 )
84 );
85 }
86  
87 my @groups;
88 my @numbers;
89  
90 for my $group ( @{ $self->{json}{groups} // [] } ) {
91 my @group_carriages;
92 for my $carriage ( @{ $group->{vehicles} // [] } ) {
93 my $carriage_object
94 = Travel::Status::DE::DBRIS::Formation::Carriage->new(
95 json => $carriage,
96 platform => {
97 start => $self->{json}{platform}{start},
98 end => $self->{json}{platform}{end},
99 }
100 );
101 push( @group_carriages, $carriage_object );
102 push( @{ $self->{carriages} }, $carriage_object );
103 }
104 @group_carriages
105 = sort { ( $a->start_percent // 0 ) <=> ( $b->start_percent // 0 ) }
106 @group_carriages;
107 my $group_obj = Travel::Status::DE::DBRIS::Formation::Group->new(
108 json => $group,
109 carriages => \@group_carriages,
110 );
111 push( @groups, $group_obj );
112 push( @numbers, $group_obj->train_no );
113 }
114  
115 @groups = sort { ( $a->start_percent // 0 ) <=> ( $b->start_percent // 0 ) }
116 @groups;
117  
118 @numbers = uniq @numbers;
119 $self->{train_numbers} = \@numbers;
120  
121 if ( @{ $self->{carriages} // [] } > 1 ) {
122 if ( defined $self->{carriages}[0]->{start_percent}
123 and defined $self->{carriages}[-1]->{start_percent}
124 and $self->{carriages}[0]->{start_percent}
125 > $self->{carriages}[-1]->{start_percent} )
126 {
127 $self->{direction} = 100;
128 }
129 else {
130 $self->{direction} = 0;
131 }
132 }
133  
134 $self->{groups} = [@groups];
135 }
136  
137 sub destinations {
138 my ($self) = @_;
139  
140 return @{ $self->{destinations} // [] };
141 }
142  
143 sub train_numbers {
144 my ($self) = @_;
145  
146 return @{ $self->{train_numbers} // [] };
147 }
148  
149 sub trains {
150 my ($self) = @_;
151  
152 return @{ $self->{trains} // [] };
153 }
154  
155 sub sectors {
156 my ($self) = @_;
157  
158 return @{ $self->{sectors} // [] };
159 }
160  
161 sub groups {
162 my ($self) = @_;
163 return @{ $self->{groups} // [] };
164 }
165  
166 sub carriages {
167 my ($self) = @_;
168 return @{ $self->{carriages} // [] };
169 }
170  
171 sub TO_JSON {
172 my ($self) = @_;
173  
174 my $ret = { %{$self} };
175  
176 return $ret;
177 }
178  
179 1;