File Coverage

blib/lib/Map/Metro/Graph/Station.pm
Criterion Covered Total %
statement 27 43 62.7
branch 2 10 20.0
condition n/a
subroutine 9 12 75.0
pod 0 5 0.0
total 38 70 54.2


line stmt bran cond sub pod time code
1 2     2   22 use 5.10.0;
  2         4  
2 2     2   8 use strict;
  2         3  
  2         39  
3 2     2   7 use warnings;
  2         2  
  2         94  
4              
5             package Map::Metro::Graph::Station;
6              
7             # ABSTRACT: Information about a station
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.2405';
10              
11 2     2   6 use Map::Metro::Elk;
  2         3  
  2         11  
12 2     2   2615 use Types::Standard qw/Int Str Maybe ArrayRef Bool/;
  2         3  
  2         12  
13 2     2   1662 use Map::Metro::Types qw/Line Station/;
  2         1515  
  2         123  
14 2     2   1431 use Text::Undiacritic 'undiacritic';
  2         328143  
  2         1522  
15              
16             has id => (
17             is => 'ro',
18             isa => Int,
19             required => 1,
20             documentation => 'Internal identification',
21             );
22              
23             has name => (
24             is => 'rw',
25             isa => Str,
26             required => 1,
27             documentation => q{The station's name, with any diacritics removed.},
28             );
29             has original_name => (
30             is => 'ro',
31             isa => Maybe[Str],
32             documentation => q{The station's name as given in the map file.},
33             );
34             has search_names => (
35             is => 'rw',
36             isa => ArrayRef[Str],
37             traits => ['Array'],
38             default => sub { [] },
39             handles => {
40             add_search_name => 'push',
41             all_search_names => 'elements',
42             },
43             documentation => q{All search names for the station given in the map file.},
44             );
45             has alternative_names => (
46             is => 'rw',
47             isa => ArrayRef[Str],
48             traits => ['Array'],
49             default => sub { [] },
50             handles => {
51             add_alternative_name => 'push',
52             all_alternative_names => 'elements',
53             },
54             documentation => q{All alternative names for the station given in the map file.},
55             );
56              
57             has lines => (
58             is => 'rw',
59             isa => ArrayRef[ Line ],
60             traits => ['Array'],
61             default => sub { [] },
62             init_arg => undef,
63             handles => {
64             add_line => 'push',
65             all_lines => 'elements',
66             find_line => 'first',
67             filter_lines => 'grep',
68             },
69             documentation => q{All lines passing through this station.},
70             );
71             has connecting_stations => (
72             is => 'ro',
73             isa => ArrayRef[ Station ],
74             traits => ['Array'],
75             default => sub { [] },
76             init_arg => undef,
77             handles => {
78             add_connecting_station => 'push',
79             all_connecting_stations => 'elements',
80             find_connecting_station => 'first',
81             },
82             documentation => q{All stations one can travel to from this station without passing another station.},
83             );
84             has do_undiacritic => (
85             is => 'rw',
86             isa => Bool,
87             default => 1,
88             traits => ['Documented'],
89             documentation_alts => {
90             0 => q{Do not remove diacritics from station name.},
91             1 => q{Do remove diacritics from station name.},
92             },
93             );
94              
95             around BUILDARGS => sub {
96             my $orig = shift;
97             my $class = shift;
98             my %args = @_;
99              
100             return $class->$orig(%args) if exists $args{'do_undiacritic'} && !$args{'do_undiacritic'};
101              
102             my $no_diacritic = undiacriticise($args{'name'});
103             if(defined $no_diacritic) {
104             if(exists $args{'search_names'}) {
105             push @{ $args{'search_names'} } => $no_diacritic;
106             }
107             else {
108             $args{'search_names'} = [$no_diacritic];
109             }
110             }
111             return $class->$orig(%args);
112             };
113              
114             sub set_name {
115 0     0 0 0 my $self = shift;
116 0         0 my $name = shift;
117              
118 0 0       0 if($self->do_undiacritic) {
119 0         0 my $no_diacritic = undiacriticise($name);
120 0 0       0 if(defined $no_diacritic) {
121 0         0 $self->add_search_name($no_diacritic);
122             }
123             }
124 0         0 $self->name($name);
125             }
126             sub set_original_name {
127 0     0 0 0 my $self = shift;
128 0         0 my $name = shift;
129              
130 0 0       0 if($self->do_undiacritic) {
131 0         0 my $no_diacritic = undiacriticise($name);
132              
133 0 0       0 if(defined $no_diacritic) {
134 0         0 $self->add_search_name($no_diacritic);
135             }
136             }
137 0         0 $self->original_name($name);
138             }
139             around add_search_name => sub {
140             my $next = shift;
141             my $self = shift;
142             my @names = @_;
143              
144             if($self->do_undiacritic) {
145             foreach my $name (@names) {
146             my $no_diacritic = undiacriticise($name);
147             push @names => $no_diacritic if defined $no_diacritic;
148             }
149             }
150             $self->$next(@names);
151             };
152              
153             around add_alternative_name => sub {
154             my $next = shift;
155             my $self = shift;
156             my @names = @_;
157              
158             if($self->do_undiacritic) {
159             foreach my $name (@names) {
160             my $no_diacritic = undiacriticise($name);
161             push @names => $no_diacritic if defined $no_diacritic;
162             }
163             }
164             $self->$next(@names);
165             };
166              
167             around add_line => sub {
168             my $next = shift;
169             my $self = shift;
170             my $line = shift; # Line
171              
172             $self->$next($line) if !$self->find_line(sub { $line->id eq $_->id });
173             };
174              
175             around add_connecting_station => sub {
176             my $next = shift;
177             my $self = shift;
178             my $station = shift; # Station
179              
180             $self->$next($station) if !$self->find_connecting_station(sub { $station->id eq $_->id });
181             };
182              
183             sub name_with_alternative {
184 0     0 0 0 my $self = shift;
185              
186 0         0 return ($self->name, $self->all_alternative_names);
187             }
188              
189             sub undiacriticise {
190 80     80 0 57 my $text = shift;
191 80         171 my $undia = undiacritic($text);
192 80 100       3445 return $undia if $undia ne $text;
193 64         75 return;
194             }
195              
196             sub to_hash {
197 12     12 0 9 my $self = shift;
198              
199             return {
200             id => $self->id,
201             name => $self->name,
202             original_name => $self->original_name,
203             search_names => [ $self->all_search_names ],
204             alternative_names => [ $self->all_alternative_names ],
205             lines => [
206 12         255 map { $_->to_hash } $self->all_lines
  12         27  
207             ],
208             # connecting_stations => [
209             # map { $_->to_hash } $self->all_connecting_stations
210             # ],
211              
212             }
213             }
214              
215             __PACKAGE__->meta->make_immutable;
216              
217             1;
218              
219             __END__
220              
221             =pod
222              
223             =encoding UTF-8
224              
225             =head1 NAME
226              
227             Map::Metro::Graph::Station - Information about a station
228              
229             =head1 VERSION
230              
231             Version 0.2405, released 2016-07-23.
232              
233              
234              
235             =head1 DESCRIPTION
236              
237             Stations represents actual stations, and are used both during the graph building phase and the navigational phase.
238              
239             =head1 ATTRIBUTES
240              
241              
242             =head2 id
243              
244             =begin HTML
245              
246             <table cellpadding="0" cellspacing="0">
247             <tr>
248             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Int">Int</a></td>
249             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">required</td>
250             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
251             </tr>
252             </table>
253              
254             <p></p>
255              
256             =end HTML
257              
258             =begin markdown
259              
260             <table cellpadding="0" cellspacing="0">
261             <tr>
262             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Int">Int</a></td>
263             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">required</td>
264             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
265             </tr>
266             </table>
267              
268             <p></p>
269              
270             =end markdown
271              
272             =head2 name
273              
274             =begin HTML
275              
276             <table cellpadding="0" cellspacing="0">
277             <tr>
278             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Str">Str</a></td>
279             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">required</td>
280             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
281             </tr>
282             </table>
283              
284             <p></p>
285              
286             =end HTML
287              
288             =begin markdown
289              
290             <table cellpadding="0" cellspacing="0">
291             <tr>
292             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Str">Str</a></td>
293             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">required</td>
294             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
295             </tr>
296             </table>
297              
298             <p></p>
299              
300             =end markdown
301              
302             =head2 do_undiacritic
303              
304             =begin HTML
305              
306             <table cellpadding="0" cellspacing="0">
307             <tr>
308             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Bool">Bool</a></td>
309             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>1</code></td>
310             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">read/write</td>
311             <td style="text-align: right; padding-right: 6px; padding-left: 6px;"><code>0</code>:</td>
312             <td style="padding-left: 12px;">Do not remove diacritics from station name.</td>
313             </tr>
314             <tr>
315             <td>&#160;</td>
316             <td>&#160;</td>
317             <td>&#160;</td>
318             <td style="text-align: right; padding-right: 6px; padding-left: 6px;"><code>1</code>:</td>
319             <td style="padding-left: 12px;">Do remove diacritics from station name.</td>
320             </tr>
321             </table>
322              
323             <p></p>
324              
325             =end HTML
326              
327             =begin markdown
328              
329             <table cellpadding="0" cellspacing="0">
330             <tr>
331             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Bool">Bool</a></td>
332             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>1</code></td>
333             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">read/write</td>
334             <td style="text-align: right; padding-right: 6px; padding-left: 6px;"><code>1</code>:</td>
335             <td style="padding-left: 12px;">Do remove diacritics from station name.</td>
336             </tr>
337             </table>
338              
339             <p></p>
340              
341             =end markdown
342              
343             =head2 alternative_names
344              
345             =begin HTML
346              
347             <table cellpadding="0" cellspacing="0">
348             <tr>
349             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
350             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code></td>
351             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
352             </tr>
353             </table>
354              
355             <p></p>
356              
357             =end HTML
358              
359             =begin markdown
360              
361             <table cellpadding="0" cellspacing="0">
362             <tr>
363             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
364             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code></td>
365             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
366             </tr>
367             </table>
368              
369             <p></p>
370              
371             =end markdown
372              
373             =head2 original_name
374              
375             =begin HTML
376              
377             <table cellpadding="0" cellspacing="0">
378             <tr>
379             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Maybe">Maybe</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
380             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
381             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
382             </tr>
383             </table>
384              
385             <p></p>
386              
387             =end HTML
388              
389             =begin markdown
390              
391             <table cellpadding="0" cellspacing="0">
392             <tr>
393             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Maybe">Maybe</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
394             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
395             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
396             </tr>
397             </table>
398              
399             <p></p>
400              
401             =end markdown
402              
403             =head2 search_names
404              
405             =begin HTML
406              
407             <table cellpadding="0" cellspacing="0">
408             <tr>
409             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
410             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code></td>
411             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
412             </tr>
413             </table>
414              
415             <p></p>
416              
417             =end HTML
418              
419             =begin markdown
420              
421             <table cellpadding="0" cellspacing="0">
422             <tr>
423             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
424             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code></td>
425             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
426             </tr>
427             </table>
428              
429             <p></p>
430              
431             =end markdown
432              
433             =head2 connecting_stations
434              
435             =begin HTML
436              
437             <table cellpadding="0" cellspacing="0">
438             <tr>
439             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Map::Metro::Types#Station">Station</a> ]</td>
440             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
441             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
442             </tr>
443             </table>
444              
445             <p></p>
446              
447             =end HTML
448              
449             =begin markdown
450              
451             <table cellpadding="0" cellspacing="0">
452             <tr>
453             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Map::Metro::Types#Station">Station</a> ]</td>
454             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
455             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td>
456             </tr>
457             </table>
458              
459             <p></p>
460              
461             =end markdown
462              
463             =head2 lines
464              
465             =begin HTML
466              
467             <table cellpadding="0" cellspacing="0">
468             <tr>
469             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Line">Line</a> ]</td>
470             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
471             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
472             </tr>
473             </table>
474              
475             <p></p>
476              
477             =end HTML
478              
479             =begin markdown
480              
481             <table cellpadding="0" cellspacing="0">
482             <tr>
483             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Line">Line</a> ]</td>
484             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
485             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read/write</td>
486             </tr>
487             </table>
488              
489             <p></p>
490              
491             =end markdown
492              
493             =head1 METHODS
494              
495             =head2 id()
496              
497             Returns the internal station id. Do not depend on this between executions.
498              
499             =head2 name()
500              
501             Returns the station name given in the parsed map file.
502              
503             =head2 lines()
504              
505             Returns an array of all L<Lines|Map::Metro::Graph::Line> passing through the station.
506              
507             =head2 connecting_stations()
508              
509             Returns an array of all L<Stations|Map::Metro::Graph::Station> directly (on at least one line) connected to this station.
510              
511             =head1 SOURCE
512              
513             L<https://github.com/Csson/p5-Map-Metro>
514              
515             =head1 HOMEPAGE
516              
517             L<https://metacpan.org/release/Map-Metro>
518              
519             =head1 AUTHOR
520              
521             Erik Carlsson <info@code301.com>
522              
523             =head1 COPYRIGHT AND LICENSE
524              
525             This software is copyright (c) 2016 by Erik Carlsson.
526              
527             This is free software; you can redistribute it and/or modify it under
528             the same terms as the Perl 5 programming language system itself.
529              
530             =cut