Map-Tube-Beijing
view release on metacpan or search on metacpan
lib/Map/Tube/Beijing.pm view on Meta::CPAN
my %nametypes = map { $_ => 1 } qw(alt); # The permissible alternative nametypes. In our case, just 'alt'
has xml => ( is => 'ro', lazy => 1, default => sub { return dist_file('Map-Tube-Beijing', 'beijing-map.xml') } );
has nametype => ( is => 'ro', default => '',
isa => sub { die __PACKAGE__ . ": ERROR: Invalid nametype for constructor: '$_[0]'" unless ( ( $_[0] eq '') || exists($nametypes{ $_[0] } ) ) },
);
with 'Map::Tube';
before _validate_map_structure => sub {
$_[1] = _relocate_alternatives( $_[1], '_' . $_[0]->{nametype} ) if ( exists( $_[0]->{nametype}) && ( $_[0]->{nametype} ne '' ) );
$_[1] = _remove_alternatives( $_[1] );
};
sub _relocate_alternatives {
my( $branch, $suffix ) = @_;
for my $key( keys %{ $branch } ) {
if ( ref( $branch->{$key} ) eq 'HASH' ) {
$branch->{$key} = _relocate_alternatives( $branch->{$key}, $suffix );
} elsif ( ( ref( $branch->{$key} ) eq '' ) && ( $key eq ( 'name' . $suffix ) ) ) {
$branch->{'name'} = $branch->{ 'name' . $suffix };
} elsif ( ref( $branch->{$key} ) eq 'ARRAY' ) {
$branch->{$key} = [ map { _relocate_alternatives( $_, $suffix ) } @{ $branch->{$key} } ];
}
}
return $branch;
}
sub _remove_alternatives {
my($branch) = @_;
for my $key( keys %{ $branch } ) {
if ( ref( $branch->{$key} ) eq 'HASH' ) {
$branch->{$key} = _remove_alternatives( $branch->{$key} );
( run in 1.200 second using v1.01-cache-2.11-cpan-71847e10f99 )