| 1 | package WWW::EFA::DeparturesResult; | ||||||||||
| 2 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use MooseX::Params::Validate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | WWW::EFA::DeparturesResult - Store the results from a departures query | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 0.01 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = '0.01'; | |||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | TODO: RCL 2012-01-22 Documentation | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 2 | with 'WWW::EFA::Roles::Printable'; # provides string | |||||||||
| 25 | |||||||||||
| 26 | subtype 'ValidDepartureStatus', | ||||||||||
| 27 | as 'Str', | ||||||||||
| 28 | 0 | ▶ | where { $_ =~ m/^(OK|INVALID_STATION|SERVICE_DOWN)$/ }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 29 | 0 | ▶ | message { "Invalid departure status" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 30 | |||||||||||
| 31 | =head1 ATTRIBUTES | ||||||||||
| 32 | |||||||||||
| 33 | TODO: RCL 2012-01-22 Documentation | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | 2 | has 'status' => ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | isa => 'ValidDepartureStatus', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | has 'departure_stations' => ( | ||||||||||
| 43 | is => 'rw', | ||||||||||
| 44 | isa => 'HashRef[WWW::EFA::Station]', | ||||||||||
| 45 | 0 | ▶ | default => sub{ {} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 2 | ); | |||||||||
| 47 | |||||||||||
| 48 | has 'departures' => ( | ||||||||||
| 49 | is => 'rw', | ||||||||||
| 50 | isa => 'ArrayRef[WWW::EFA::Departure]', | ||||||||||
| 51 | 0 | ▶ | default => sub{ [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | 2 | ); | |||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | has 'lines' => ( | ||||||||||
| 56 | is => 'rw', | ||||||||||
| 57 | isa => 'HashRef[WWW::EFA::Line]', | ||||||||||
| 58 | 0 | ▶ | default => sub{ {} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 59 | 2 | ); | |||||||||
| 60 | |||||||||||
| 61 | =head1 METHODS | ||||||||||
| 62 | |||||||||||
| 63 | =head2 add_departure_station | ||||||||||
| 64 | |||||||||||
| 65 | =head3 Params | ||||||||||
| 66 | |||||||||||
| 67 | =over 4 | ||||||||||
| 68 | |||||||||||
| 69 | =item L<WWW::EFA::Station> | ||||||||||
| 70 | |||||||||||
| 71 | =back | ||||||||||
| 72 | |||||||||||
| 73 | Add a single L<WWW::EFA::Station> to the departure stations of this result | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | 0 | ▶ | sub add_departure_station { | ||||||||
|
Subroutine
add_departure_station: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 77 | 0 | my $self = shift; | |||||||||
| 78 | 0 | my ( $station ) = pos_validated_list( | |||||||||
| 79 | \@_, | ||||||||||
| 80 | { isa => 'WWW::EFA::Station' }, | ||||||||||
| 81 | ); | ||||||||||
| 82 | 0 | ▶ | if( not $station->location->id ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | # TODO: RCL 2011-11-20 Make a debug output here when logging enabled | ||||||||||
| 84 | # carp( "Cannot add_departure_station with a location without an id" ); | ||||||||||
| 85 | 0 | return; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | # Make sure we don't add the same location twice | ||||||||||
| 89 | 0 | ▶ | if( not $self->departure_stations->{ $station->location->id } ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | $self->departure_stations->{ $station->location->id } = $station; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | return; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 get_departure_station | ||||||||||
| 97 | |||||||||||
| 98 | Returns a L<WWW::EFA::Station> if found, else undef | ||||||||||
| 99 | |||||||||||
| 100 | =head3 Params | ||||||||||
| 101 | |||||||||||
| 102 | =over 4 | ||||||||||
| 103 | |||||||||||
| 104 | =item I<$station_id> (integer) | ||||||||||
| 105 | |||||||||||
| 106 | =back | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | 0 | ▶ | sub get_departure_station { | ||||||||
|
Subroutine
get_departure_station: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 110 | 0 | my $self = shift; | |||||||||
| 111 | 0 | my ( $station_id ) = pos_validated_list( | |||||||||
| 112 | \@_, | ||||||||||
| 113 | { isa => 'Int' }, | ||||||||||
| 114 | ); | ||||||||||
| 115 | 0 | return $self->departure_stations->{ $station_id }; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | =head2 add_line | ||||||||||
| 119 | |||||||||||
| 120 | =head3 Params | ||||||||||
| 121 | |||||||||||
| 122 | =over 4 | ||||||||||
| 123 | |||||||||||
| 124 | =item L<WWW::EFA::Line> | ||||||||||
| 125 | |||||||||||
| 126 | =back | ||||||||||
| 127 | |||||||||||
| 128 | Add a single L<WWW::EFA::Line> to this result | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | 0 | ▶ | sub add_line { | ||||||||
|
Subroutine
add_line: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 132 | 0 | my $self = shift; | |||||||||
| 133 | 0 | my ( $line ) = pos_validated_list( | |||||||||
| 134 | \@_, | ||||||||||
| 135 | { isa => 'WWW::EFA::Line' }, | ||||||||||
| 136 | ); | ||||||||||
| 137 | |||||||||||
| 138 | # Make sure we don't add the same line twice | ||||||||||
| 139 | 0 | ▶ | if( not $self->lines->{ $line->id } ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | $self->lines->{ $line->id } = $line; | |||||||||
| 141 | } | ||||||||||
| 142 | 0 | return; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | =head2 get_line | ||||||||||
| 146 | |||||||||||
| 147 | Returns a L<WWW::EFA::line> from this result by its id | ||||||||||
| 148 | |||||||||||
| 149 | =head3 Params | ||||||||||
| 150 | |||||||||||
| 151 | =over 4 | ||||||||||
| 152 | |||||||||||
| 153 | =item I<$integer> | ||||||||||
| 154 | |||||||||||
| 155 | =back | ||||||||||
| 156 | |||||||||||
| 157 | =cut | ||||||||||
| 158 | 0 | ▶ | sub get_line { | ||||||||
|
Subroutine
get_line: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 159 | 0 | my $self = shift; | |||||||||
| 160 | 0 | my $line_id = shift; | |||||||||
| 161 | 0 | return $self->lines->{ $line_id }; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | =head2 add_departure | ||||||||||
| 165 | |||||||||||
| 166 | Add a L<WWW::EFA::Departure> to this result | ||||||||||
| 167 | |||||||||||
| 168 | =head3 Params | ||||||||||
| 169 | |||||||||||
| 170 | =over 4 | ||||||||||
| 171 | |||||||||||
| 172 | =item L<WWW::EFA::Departure> | ||||||||||
| 173 | |||||||||||
| 174 | =back | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | 0 | ▶ | sub add_departure { | ||||||||
|
Subroutine
add_departure: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 178 | 0 | my $self = shift; | |||||||||
| 179 | 0 | my ( $departure ) = pos_validated_list( | |||||||||
| 180 | \@_, | ||||||||||
| 181 | { isa => 'WWW::EFA::Departure' }, | ||||||||||
| 182 | ); | ||||||||||
| 183 | |||||||||||
| 184 | 0 | ▶ | if( not $self->get_departure_station( $departure->stop_id ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 185 | # TODO: RCL 2011-11-20 Make this a debug message | ||||||||||
| 186 | # carp( sprintf "Cannot add departure because I don't know the stop_id: %s", $departure->stop_id ); | ||||||||||
| 187 | 0 | return; | |||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | 0 | ▶ | if( not $self->get_line( $departure->line_id ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | # TODO: RCL 2011-11-20 Make this a debug message | ||||||||||
| 192 | # carp( sprintf 'Cannot add departure. Line id unknown: "%s"', $departure->line_id ); | ||||||||||
| 193 | 0 | return; | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | 0 | push( @{ $self->departures }, $departure ); | |||||||||
| 197 | # TODO: RCL 2011-11-09 Sort departures by departure time? | ||||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | 2 | 1; | |||||||||
| 201 | =head1 COPYRIGHT | ||||||||||
| 202 | |||||||||||
| 203 | Copyright 2011, Robin Clarke, Munich, Germany | ||||||||||
| 204 | |||||||||||
| 205 | =head1 AUTHOR | ||||||||||
| 206 | |||||||||||
| 207 | Robin Clarke <perl@robinclarke.net> | ||||||||||
| 208 | |||||||||||
| 209 | |||||||||||