File Coverage

blib/lib/Map/Metro/Graph/Route.pm
Criterion Covered Total %
statement 32 38 84.2
branch 2 4 50.0
condition n/a
subroutine 10 12 83.3
pod 1 5 20.0
total 45 59 76.2


line stmt bran cond sub pod time code
1 2     2   18 use 5.10.0;
  2         6  
2 2     2   7 use strict;
  2         3  
  2         34  
3 2     2   6 use warnings;
  2         3  
  2         92  
4              
5             package Map::Metro::Graph::Route;
6              
7             # ABSTRACT: A sequence of line stations
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.2404';
10              
11 2     2   6 use Map::Metro::Elk;
  2         2  
  2         12  
12 2     2   2608 use Types::Standard qw/ArrayRef Str/;
  2         2  
  2         14  
13 2     2   1147 use Map::Metro::Types qw/Step LineStation/;
  2         2  
  2         10  
14 2     2   616 use List::Util qw/sum/;
  2         2  
  2         732  
15              
16             has steps => (
17             is => 'ro',
18             isa => ArrayRef[ Step ],
19             traits => ['Array'],
20             predicate => 1,
21             handles => {
22             add_step => 'push',
23             step_count => 'count',
24             get_step => 'get',
25             all_steps => 'elements',
26             filter_steps => 'grep',
27             }
28             );
29             has id => (
30             is => 'ro',
31             isa => Str,
32             init_arg => undef,
33             default => sub { join '' => map { ('a'..'z', 2..9)[int rand 33] } (1..8) },
34             );
35             has line_stations => (
36             is => 'ro',
37             isa => ArrayRef[ LineStation ],
38             traits => ['Array'],
39             handles => {
40             add_line_station => 'push',
41             all_line_stations => 'elements',
42             step => 'get',
43             line_station_count => 'count',
44             },
45             );
46              
47             sub weight {
48 0     0 1 0 my $self = shift;
49              
50 0         0 return sum map { $_->weight } $self->all_steps;
  0         0  
51             }
52              
53             sub transfer_on_final_station {
54 1     1 0 2 my $self = shift;
55              
56 1 50       29 return 0 if $self->step_count < 2;
57 1         28 my $final_step = $self->get_step(-1);
58              
59 1         23 return $final_step->origin_line_station->station->id == $final_step->destination_line_station->station->id;
60             }
61             sub transfer_on_first_station {
62 1     1 0 2 my $self = shift;
63              
64 1 50       30 return 0 if $self->step_count < 2;
65 1         30 my $first_step = $self->get_step(0);
66              
67 1         30 return $first_step->origin_line_station->station->id == $first_step->destination_line_station->station->id;
68             }
69             sub longest_line_name_length {
70 0     0 0 0 my $self = shift;
71              
72 0         0 return length((sort { length $b->origin_line_station->line->name <=> length $a->origin_line_station->line->name } $self->all_steps)[0]->origin_line_station->line->name);
  0         0  
73             }
74              
75             sub to_hash {
76 1     1 0 1 my $self = shift;
77              
78             return {
79             id => $self->id,
80             steps => [
81 3         7 map { $_->to_hash } $self->all_steps,
82             ],
83             line_stations => [
84 1         24 map { $_->to_hash } $self->all_line_stations,
  4         8  
85             ],
86             };
87             }
88              
89             __PACKAGE__->meta->make_immutable;
90              
91             1;
92              
93             __END__
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             Map::Metro::Graph::Route - A sequence of line stations
102              
103             =head1 VERSION
104              
105             Version 0.2404, released 2016-04-30.
106              
107             =head1 DESCRIPTION
108              
109             A route is a specific sequence of L<Steps|Map::Metro::Graph::Step> from one L<LineStation|Map::Metro::Graph::LineStation> to another.
110              
111             =head1 METHODS
112              
113             =head2 all_steps()
114              
115             Returns an array of the L<Steps|Map::Metro::Graph::Step> in the route, in the order they are travelled.
116              
117             =head2 weight()
118              
119             Returns an integer representing the total 'cost' of all L<Connections|Map::Metro::Graph::Connection> on this route.
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