Geography-JapanesePrefectures-Walker
view release on metacpan or search on metacpan
lib/Geography/JapanesePrefectures/Walker.pm view on Meta::CPAN
package Geography::JapanesePrefectures::Walker;
use strict;
use warnings;
use Carp;
use Scalar::Util qw(blessed);
use Encode;
use List::MoreUtils qw/uniq firstval/;
use Geography::JapanesePrefectures;
use Data::Visitor::Callback;
our $VERSION = '0.02';
sub new {
my $class = shift;
my $param = {
encoding => shift || 'utf8',
};
my $self = bless $param, $class;
$self->{_geo_data} = $self->_encode_prefectures_infos;
$self;
}
sub _encode_prefectures_infos {
my $self = shift;
my $prefs = Geography::JapanesePrefectures->prefectures_infos;
my $visitor = Data::Visitor::Callback->new(
plain_value => sub {
Encode::from_to($_, 'utf8', $self->{encoding}, 1);
}
);
$visitor->visit($prefs);
return $prefs;
}
sub prefectures_infos { shift->{_geo_data} }
sub prefectures {
my $self = shift;
return [ map { {
id => $_->{id} ,
name => $_->{name},
region => $_->{region},
} } @{ $self->prefectures_infos } ];
}
sub prefectures_name_for_id {
my ($self, $id) = @_;
my $pref = firstval { $_->{id} } grep { $_->{id} eq $id } @{ $self->prefectures_infos };
return $pref->{name};
}
sub prefectures_name {
my $self = shift;
return map { $_->{name} } @{ $self->prefectures_infos };
}
sub prefectures_regions {
my $self = shift;
return uniq map { $_->{region} } @{ $self->prefectures_infos };
}
sub prefectures_name_for_region {
my ($self, $region) = @_;
return map { $_->{name} }
grep { $_->{region} eq $region }
@{ $self->prefectures_infos };
}
sub prefectures_id_for_name {
my ($self, $name) = @_;
my $pref = firstval { $_->{id} } grep { $_->{name} eq $name } @{ $self->prefectures_infos };
return $pref->{id};
}
=head1 NAME
Geography::JapanesePrefectures::Walker - Geography::JapanesePrefectures's wrappers.
=head1 SYNOPSIS
in your script:
use Geography::JapanesePrefectures::Walker;
my $g = Geography::JapanesePrefectures::Walker->new('euc-jp');
my $prefs = $g->prefectures;
=head1 METHODS
=head2 new
create Geography::JapanesePrefectures::Walker's object.
=head2 _encode_prefectures_infos
privete method.
this method encode all data.
=head2 prefectures_infos
This method get Geography::JapanesePrefectures's all data.
=head2 prefectures
This method get Geography::JapanesePrefectures's all data.
=head2 prefectures_name_for_id
This method get Geography::JapanesePrefectures's name data for id.
=head2 prefectures_name
This method get Geography::JapanesePrefectures's name data.
=head2 prefectures_regions
This method get Geography::JapanesePrefectures's region data.
=head2 prefectures_name_for_region
This method get Geography::JapanesePrefectures's name data for region.
=head2 prefectures_id_for_name
This method get Geography::JapanesePrefectures's id data for name.
=head1 SEE ALSO
L<Geography::JapanesePrefectures>
L<Plagger::Walker>
=head1 THANKS TO
The authors of Plagger::Walker, from which a lot of code was used.
id:tokuhirom
=head1 AUTHOR
Atsushi Kobayashi, C<< <nekokak at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-geography-japaneseprefectures-walker at rt.cpan.org>, or through the web interface at
( run in 3.372 seconds using v1.01-cache-2.11-cpan-5735350b133 )