| 1 | package Geo::Region; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use v5.8.1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Scalar::Util qw( looks_like_number weaken ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use List::Util qw( all any ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = '0.00_01'; | |||||||||
| 10 | |||||||||||
| 11 | 2 | my %children_of = ( | |||||||||
| 12 | # regions of subregions | ||||||||||
| 13 | '001' => [qw( 002 009 019 142 150 )], | ||||||||||
| 14 | '002' => [qw( 011 014 015 017 018 )], | ||||||||||
| 15 | '003' => [qw( 013 021 029 )], | ||||||||||
| 16 | '009' => [qw( 053 054 057 061 QO )], | ||||||||||
| 17 | '019' => [qw( 003 005 013 021 029 419 )], | ||||||||||
| 18 | '142' => [qw( 030 034 035 143 145 )], | ||||||||||
| 19 | '150' => [qw( 039 151 154 155 EU QU )], | ||||||||||
| 20 | '419' => [qw( 005 013 029 )], | ||||||||||
| 21 | # regions of countries and territories | ||||||||||
| 22 | '005' => [qw( AR BO BR CL CO EC FK GF GY PE PY SR UY VE )], | ||||||||||
| 23 | '011' => [qw( BF BJ CI CV GH GM GN GW LR ML MR NE NG SH SL SN TG )], | ||||||||||
| 24 | '013' => [qw( BZ CR GT HN MX NI PA SV )], | ||||||||||
| 25 | '014' => [qw( BI DJ ER ET KE KM MG MU MW MZ RE RW SC SO TZ UG YT ZM ZW )], | ||||||||||
| 26 | '015' => [qw( DZ EA EG EH IC LY MA SD SS TN )], | ||||||||||
| 27 | '017' => [qw( AO CD CF CG CM GA GQ ST TD ZR )], | ||||||||||
| 28 | '018' => [qw( BW LS NA SZ ZA )], | ||||||||||
| 29 | '021' => [qw( BM CA GL PM US )], | ||||||||||
| 30 | '029' => [qw( AG AI AN AW BB BL BQ BS CU CW DM DO GD GP HT JM KN KY LC MF MQ MS PR SX TC TT VC VG VI )], | ||||||||||
| 31 | '030' => [qw( CN HK JP KP KR MN MO TW )], | ||||||||||
| 32 | '034' => [qw( AF BD BT IN IR LK MV NP PK )], | ||||||||||
| 33 | '035' => [qw( BN BU ID KH LA MM MY PH SG TH TL TP VN )], | ||||||||||
| 34 | '039' => [qw( AD AL BA CS ES GI GR HR IT ME MK MT PT RS SI SM VA XK YU )], | ||||||||||
| 35 | '053' => [qw( AU NF NZ )], | ||||||||||
| 36 | '054' => [qw( FJ NC PG SB VU )], | ||||||||||
| 37 | '057' => [qw( FM GU KI MH MP NR PW )], | ||||||||||
| 38 | '061' => [qw( AS CK NU PF PN TK TO TV WF WS )], | ||||||||||
| 39 | '143' => [qw( KG KZ TJ TM UZ )], | ||||||||||
| 40 | '145' => [qw( AE AM AZ BH CY GE IL IQ JO KW LB NT OM PS QA SA SY TR YD YE )], | ||||||||||
| 41 | '151' => [qw( BG BY CZ HU MD PL RO RU SK SU UA )], | ||||||||||
| 42 | '154' => [qw( AX DK EE FI FO GB GG IE IM IS JE LT LV NO SE SJ )], | ||||||||||
| 43 | '155' => [qw( AT BE CH DD DE FR FX LI LU MC NL )], | ||||||||||
| 44 | 'EU' => [qw( AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK )], | ||||||||||
| 45 | 'QO' => [qw( AC AQ BV CC CP CX DG GS HM IO TA TF UM )], | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | # codes excluded from country list due to being deprecated or grouping container | ||||||||||
| 49 | 2 | my %noncountries = map { $_ => 1 } qw( | |||||||||
| 50 | AN BU CS DD EU FX NT QO QU SU TP YD YU ZR | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | sub coerce_regions { | ||||||||||
| 54 | 11 | ▶ | map { looks_like_number $_ ? sprintf('%03d', $_) : uc } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 11 | grep { defined } | |||||||||
| 56 | 13 | ▶ | map { ref eq 'ARRAY' ? @$_ : $_ } @_ | ||||||||
|
Subroutine
coerce_regions: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | |||||||||||
| 61 | has _regions => ( | ||||||||||
| 62 | is => 'ro', | ||||||||||
| 63 | 2 | ▶ | coerce => sub { [ coerce_regions(shift) ] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 2 | init_arg => 'region', | |||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | has _excludes => ( | ||||||||||
| 69 | is => 'ro', | ||||||||||
| 70 | 2 | ▶ | coerce => sub { [ coerce_regions(shift) ] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 2 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 2 | init_arg => 'exclude', | |||||||||
| 73 | ); | ||||||||||
| 74 | |||||||||||
| 75 | has _children => ( | ||||||||||
| 76 | is => 'lazy', | ||||||||||
| 77 | builder => sub { | ||||||||||
| 78 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 1 | my $build_children; | |||||||||
| 80 | |||||||||||
| 81 | $build_children = sub { map { | ||||||||||
| 82 | 3 | ▶ | $_, exists $children_of{$_} | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 83 | 30 | ▶ | ? $build_children->(@{$children_of{$_}}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | : () | ||||||||||
| 85 | 1 | } @_ }; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my %excludes = map { $_ => 1 } | |||||||||
| 88 | 1 | $build_children->(@{$self->_excludes}); | |||||||||
| 89 | |||||||||||
| 90 | 30 | my %children = map { $_ => 1 } | |||||||||
| 91 | 30 | grep { !exists $excludes{$_} } | |||||||||
| 92 | 1 | $build_children->(@{$self->_regions}); | |||||||||
| 93 | |||||||||||
| 94 | 1 | weaken $build_children; | |||||||||
| 95 | 1 | return \%children; | |||||||||
| 96 | }, | ||||||||||
| 97 | 2 | ); | |||||||||
| 98 | |||||||||||
| 99 | has _parents => ( | ||||||||||
| 100 | is => 'lazy', | ||||||||||
| 101 | builder => sub { | ||||||||||
| 102 | 1 | ▶ | my @regions = @{shift->_regions}; | ||||||||
|
Subroutine
_build__parents: called CC=1 SCAR=0.0
| |||||||||||
| 103 | 1 | my ($build_parents, %count); | |||||||||
| 104 | |||||||||||
| 105 | $build_parents = sub { map { | ||||||||||
| 106 | 10 | ▶ | my $region = $_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 107 | $region, $build_parents->(grep { | ||||||||||
| 108 | 9 | ▶ | any { $_ eq $region } @{$children_of{$_}} | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 109 | } keys %children_of); | ||||||||||
| 110 | 1 | } @_ }; | |||||||||
| 111 | |||||||||||
| 112 | 5 | my %parents = map { $_ => 1 } | |||||||||
| 113 | 1 | grep { ++$count{$_} == @regions } | |||||||||
| 114 | $build_parents->(@regions); | ||||||||||
| 115 | |||||||||||
| 116 | 1 | weaken $build_parents; | |||||||||
| 117 | 1 | return \%parents; | |||||||||
| 118 | }, | ||||||||||
| 119 | 2 | ); | |||||||||
| 120 | |||||||||||
| 121 | has _countries => ( | ||||||||||
| 122 | is => 'lazy', | ||||||||||
| 123 | builder => sub { [ | ||||||||||
| 124 | sort | ||||||||||
| 125 | 30 | ▶ | grep { /^[A-Z]{2}$/ && !exists $noncountries{$_} } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 1 | ▶ | keys %{shift->_children} | ||||||||
|
Subroutine
_build__countries: called CC=2 SCAR=6.9
| |||||||||||
| 127 | ] }, | ||||||||||
| 128 | 2 | ); | |||||||||
| 129 | |||||||||||
| 130 | sub contains { | ||||||||||
| 131 | 3 | ▶ | my ($self, @regions) = @_; | ||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 132 | 3 | ▶ | return all { exists $self->_children->{$_} } coerce_regions(@regions); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | sub is_within { | ||||||||||
| 136 | 6 | ▶ | my ($self, @regions) = @_; | ||||||||
|
Subroutine
is_within: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 137 | 6 | ▶ | return all { exists $self->_parents->{$_} } coerce_regions(@regions); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | sub countries { | ||||||||||
| 141 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
countries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 142 | 1 | return @{$self->_countries}; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 2 | 1; | |||||||||
| 146 | |||||||||||
| 147 | __END__ | ||||||||||