File Coverage

blib/lib/Map/Metro/Graph/Step.pm
Criterion Covered Total %
statement 22 38 57.8
branch 0 4 0.0
condition n/a
subroutine 8 12 66.6
pod 0 5 0.0
total 30 59 50.8


line stmt bran cond sub pod time code
1 2     2   24 use 5.10.0;
  2         5  
2 2     2   8 use strict;
  2         3  
  2         37  
3 2     2   6 use warnings;
  2         3  
  2         113  
4              
5             package Map::Metro::Graph::Step;
6              
7             # ABSTRACT: The movement from one station to the next in a route
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.2404';
10              
11 2     2   7 use Map::Metro::Elk;
  2         2  
  2         14  
12 2     2   2702 use Types::Standard qw/Maybe Int/;
  2         3  
  2         15  
13 2     2   1300 use Map::Metro::Types qw/LineStation Step/;
  2         3  
  2         12  
14 2     2   698 use PerlX::Maybe qw/maybe/;
  2         4  
  2         784  
15              
16             has origin_line_station => (
17             is => 'ro',
18             isa => LineStation,
19             required => 1,
20             );
21             has destination_line_station => (
22             is => 'ro',
23             isa => LineStation,
24             required => 1,
25             );
26             has previous_step => (
27             is => 'rw',
28             isa => Maybe[ Step ],
29             predicate => 1,
30             );
31             has next_step => (
32             is => 'rw',
33             isa => Maybe[ Step ],
34             predicate => 1,
35             );
36             has weight => (
37             is => 'ro',
38             isa => Int,
39             required => 1,
40             default => 1,
41             );
42              
43             around BUILDARGS => sub {
44             my $orig = shift;
45             my $class = shift;
46             my %args = @_;
47              
48             return $class->$orig(%args) if !exists $args{'from_connection'};
49              
50             my $conn = $args{'from_connection'};
51             return if !defined $conn;
52              
53             return $class->$orig(
54             origin_line_station => $conn->origin_line_station,
55             destination_line_station => $conn->destination_line_station,
56             weight => $conn->weight,
57             );
58             };
59              
60             sub is_line_transfer {
61 0     0 0 0 my $self = shift;
62              
63 0         0 return $self->origin_line_station->station->id == $self->destination_line_station->station->id;
64 0         0 return $self->origin_line_station->line->id ne $self->destination_line_station->line->id;
65             }
66             sub is_station_transfer {
67 0     0 0 0 my $self = shift;
68              
69 0         0 my $origin_station_line_ids = [ map { $_->id } $self->origin_line_station->station->all_lines ];
  0         0  
70 0         0 my $destination_station_line_ids = [ map { $_->id } $self->destination_line_station->station->all_lines ];
  0         0  
71              
72 0         0 my $are_on_same_line = List::Compare->new($origin_station_line_ids, $destination_station_line_ids)->get_intersection;
73              
74 0         0 return !$are_on_same_line;
75             }
76             sub was_line_transfer {
77 0     0 0 0 my $self = shift;
78              
79 0 0       0 return if !$self->has_previous_step;
80 0         0 return $self->previous_step->is_line_transfer;
81             }
82             sub was_station_transfer {
83 0     0 0 0 my $self = shift;
84              
85 0 0       0 return if !$self->has_previous_step;
86 0         0 return $self->previous_step->is_station_transfer;
87             }
88              
89             sub to_hash {
90 3     3 0 8 my $self = shift;
91              
92             return {
93 3         72 origin_line_station => $self->origin_line_station->to_hash,
94             destination_line_station => $self->destination_line_station->to_hash,
95             # maybe previous_step => $self->has_previous_step ? $self->previous_step->to_hash : undef,
96             # maybe next_step => $self->has_next_step ? $self->next_step->to_hash : undef,
97             weight => $self->weight,
98             };
99             }
100              
101             __PACKAGE__->meta->make_immutable;
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Map::Metro::Graph::Step - The movement from one station to the next in a route
114              
115             =head1 VERSION
116              
117             Version 0.2404, released 2016-04-30.
118              
119             =head1 DESCRIPTION
120              
121             Steps are exactly like L<Connections::Map::Metro::Graph::Connection>, in that they describe the combination of two
122             specific L<LineStations|Map::Metro::Graph::LineStation>, and the 'cost' of travelling between them, but with an important
123             difference: A Step is part of a specific L<Route|Map::Metro::Graph::Route>.
124              
125             =head1 SOURCE
126              
127             L<https://github.com/Csson/p5-Map-Metro>
128              
129             =head1 HOMEPAGE
130              
131             L<https://metacpan.org/release/Map-Metro>
132              
133             =head1 AUTHOR
134              
135             Erik Carlsson <info@code301.com>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2016 by Erik Carlsson.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut