File Coverage

blib/lib/Map/Tube/Beijing.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             # -*- perl -*-
2            
3             #
4             # Author: Gisbert W. Selke, TapirSoft Selke & Selke GbR.
5             #
6             # Copyright (C) 2015 Gisbert W. Selke. All rights reserved.
7             # This package is free software; you can redistribute it and/or
8             # modify it under the same terms as Perl itself.
9             #
10             # Mail: gws@cpan.org
11             #
12            
13            
14             package Map::Tube::Beijing;
15 1     1   21500 use strict;
  1         2  
  1         43  
16 1     1   5 use warnings;
  1         1  
  1         59  
17            
18             our $VERSION = '0.01';
19            
20             =encoding utf8
21            
22             =head1 NAME
23            
24             Map::Tube::Beijing - Interface to the Beijing tube map
25            
26             =cut
27            
28 1     1   566 use File::Share ':all';
  1         7443  
  1         161  
29 1     1   218 use XML::Simple;
  0            
  0            
30             use Moo;
31             use namespace::clean;
32            
33             has xml => ( is => 'ro', lazy => 1, default => sub { return dist_file('Map-Tube-Beijing', 'beijing-map.xml') } );
34             has nametype => ( is => 'ro', default => '',
35             isa => sub { die "Illegal nametype '$_[0]'" unless $_[0] =~ /^(alt)?$/ },
36             );
37            
38             with 'Map::Tube';
39            
40             around BUILDARGS => sub {
41             my($orig, $class, @args) = @_;
42             my %args;
43             if ( ( @args == 1 ) && ( ref($args[0]) == 'HASH' ) ) {
44             %args = %{ $args[0] };
45             } else {
46             %args = @args;
47             }
48            
49             if ( exists($args{nametype}) && ( $args{nametype} ne '' ) ) {
50             $args{xml} = XMLout( _xmlmod(
51             XMLin( dist_file('Map-Tube-Beijing', 'beijing-map.xml'),
52             KeyAttr => [ ], KeepRoot => 1,
53             ),
54             '_' . $args{nametype},
55             ),
56             XMLDecl => 1, KeepRoot => 1,
57             );
58             }
59            
60             return $class->$orig(%args);
61            
62             };
63            
64            
65             sub _xmlmod {
66             my ( $branch, $suffix ) = @_;
67             for my $key( keys %{ $branch } ) {
68             if ( ref( $branch->{$key} ) eq 'HASH' ) {
69             $branch->{$key} = _xmlmod( $branch->{$key}, $suffix );
70             } elsif ( ( ref( $branch->{$key} ) eq '' ) && ( $key eq ( 'name' . $suffix ) ) ) {
71             $branch->{'name'} = $branch->{ 'name' . $suffix };
72             } elsif ( ( ref( $branch->{$key} ) eq '' ) && ( $key eq ( 'line' . $suffix ) ) ) {
73             $branch->{'line'} = $branch->{ 'line' . $suffix };
74             } elsif ( ref( $branch->{$key} ) eq 'ARRAY' ) {
75             $branch->{$key} = [ map { _xmlmod( $_, $suffix ) } @{ $branch->{$key} } ];
76             }
77             }
78             return $branch;
79             }
80            
81             =head1 SYNOPSIS
82            
83             use Map::Tube::Beijing;
84             my $tube = Map::Tube::Beijing->new();
85            
86             my $route = $tube->get_shortest_route('Yonghegong', 'Chongwenmen');
87            
88             print "Route: $route\n";
89            
90             =head1 DESCRIPTION
91            
92             This module allows to find the shortest route between any two given tube
93             stations in Beijing. All interesting methods are provided by the role
94             L.
95            
96             =head1 METHODS
97            
98             =head2 CONSTRUCTOR
99            
100             use Map::Tube::Beijing;
101             my $tube_chin = Map::Tube::Beijing->new();
102             my $tube_pinyin = Map::Tube::Beijing->new( nametype => 'alt' );
103            
104             This will read the tube information from the shared file F,
105             which is part of the distribution. Without argument, full Chinese characters
106             (simplified) will be used. With the value C<'alt>' for C, pinyin
107             transliteration into Western characters will be used. Other values will throw
108             an error.
109            
110             =head1 ERRORS
111            
112             If something goes wrong, maybe because the map information file was corrupted,
113             the constructor will die.
114            
115             =head1 AUTHOR
116            
117             Gisbert W. Selke, TapirSoft Selke & Selke GbR.
118            
119             =head1 COPYRIGHT AND LICENCE
120            
121             The data for the XML file were mainly taken from the appropriate Wikipedia
122             pages. They are CC BY-SA 2.0.
123             The module itself is free software; you may redistribute and/or modify it under
124             the same terms as Perl itself.
125            
126             =head1 SEE ALSO
127            
128             L, L.
129            
130             =cut
131            
132             1;