File Coverage

blib/lib/Map/Metro/Graph/Routing.pm
Criterion Covered Total %
statement 20 24 83.3
branch n/a
condition n/a
subroutine 7 9 77.7
pod 0 2 0.0
total 27 35 77.1


line stmt bran cond sub pod time code
1 2     2   25 use 5.10.0;
  2         5  
2 2     2   8 use strict;
  2         2  
  2         37  
3 2     2   7 use warnings;
  2         3  
  2         95  
4              
5             package Map::Metro::Graph::Routing;
6              
7             # ABSTRACT: A collection of routes between two stations
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.2405';
10              
11 2     2   12 use Map::Metro::Elk;
  2         2  
  2         13  
12 2     2   2639 use Types::Standard qw/ArrayRef/;
  2         2  
  2         14  
13 2     2   1104 use Map::Metro::Types qw/Station LineStation Route/;
  2         3  
  2         12  
14              
15             has origin_station => (
16             is => 'ro',
17             isa => Station,
18             required => 1,
19             );
20             has destination_station => (
21             is => 'ro',
22             isa => Station,
23             required => 1,
24             );
25             has line_stations => (
26             is => 'ro',
27             isa => ArrayRef[ LineStation ],
28             traits => ['Array'],
29             default => sub {[]},
30             handles => {
31             add_line_station => 'push',
32             find_line_station => 'first',
33             all_line_stations => 'elements',
34             }
35             );
36             has routes => (
37             is => 'ro',
38             isa => ArrayRef[ Route ],
39             traits => ['Array'],
40             handles => {
41             get_route => 'get',
42             add_route => 'push',
43             all_routes => 'elements',
44             sort_routes => 'sort',
45             route_count => 'count',
46             find_route => 'first',
47             },
48             );
49              
50             around add_line_station => sub {
51             my $next = shift;
52             my $self = shift;
53             my $ls = shift; # LineStation
54              
55             my $exists = $self->find_line_station(sub { $ls->line_station_id == $_->line_station_id });
56             return if $exists;
57             $self->$next($ls);
58             };
59              
60             sub ordered_routes {
61 0     0 0 0 my $self = shift;
62              
63 0     0   0 $self->sort_routes(sub { $_[0]->weight <=> $_[1]->weight });
  0         0  
64             }
65              
66             sub to_hash {
67 1     1 0 2 my $self = shift;
68              
69             return {
70             origin_station => $self->origin_station->to_hash,
71             destination_station => $self->destination_station->to_hash,
72             line_stations => [
73 0         0 map { $_->to_hash } $self->all_line_stations,
74             ],
75             routes => [
76 1         33 map { $_->to_hash } $self->all_routes,
  1         5  
77             ],
78             };
79             }
80              
81             __PACKAGE__->meta->make_immutable;
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Map::Metro::Graph::Routing - A collection of routes between two stations
94              
95             =head1 VERSION
96              
97             Version 0.2405, released 2016-07-23.
98              
99             =head1 DESCRIPTION
100              
101             A routing is the collection of L<Routes|Map::Metro::Graph::Route> possible between two L<Stations|Map::Metro::Graph::Station>.
102              
103             =head1 METHODS
104              
105             =head2 origin_station()
106              
107             Returns the L<Station|Map::Metro::Graph::Station> object representing the starting station of the route.
108              
109             =head2 destination_station()
110              
111             Returns the L<Station|Map::Metro::Graph::Station> object representing the final station of the route.
112              
113             =head2 line_stations()
114              
115             Returns an array of all L<LineStation|Map::Metro::Graph::LineStations> possible in the routing.
116              
117             =head2 routes()
118              
119             Returns an array of all L<Route|Map::Metro::Graph::Routes> in the routing.
120              
121             =head1 SOURCE
122              
123             L<https://github.com/Csson/p5-Map-Metro>
124              
125             =head1 HOMEPAGE
126              
127             L<https://metacpan.org/release/Map-Metro>
128              
129             =head1 AUTHOR
130              
131             Erik Carlsson <info@code301.com>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2016 by Erik Carlsson.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut