blib/lib/Geo/Region.pm

filter: Statementstmt 100% 82 / 82 Branchbran 100% 10 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 21 / 21 Podpod 75.0% 3 / 4 totaltotal 99.1% 121 / 122 SCAR 19.5
CC
7
Cov
100%
BUILDARGS
5.0
__ANON__
2.0
_build__countries
2.0
CRAP 7.0
« Build ↑ Summary blib/lib/Geo/Region/Constant.pm »
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 Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util qw( looks_like_number weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::Util qw( all any );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.06';
11  
12 2 my %children_of = (
13 # regions of subregions
14 '001' => [qw( 002 009 019 142 150 )],
15 '002' => [qw( 011 014 015 017 018 )],
16 '003' => [qw( 013 021 029 )],
17 '009' => [qw( 053 054 057 061 QO )],
18 '019' => [qw( 003 005 013 021 029 419 )],
19 '142' => [qw( 030 034 035 143 145 )],
20 '150' => [qw( 039 151 154 155 EU )],
21 '419' => [qw( 005 013 029 )],
22 # regions of countries and territories
23 '005' => [qw( AR BO BR CL CO EC FK GF GY PE PY SR UY VE )],
24 '011' => [qw( BF BJ CI CV GH GM GN GW LR ML MR NE NG SH SL SN TG )],
25 '013' => [qw( BZ CR GT HN MX NI PA SV )],
26 '014' => [qw( BI DJ ER ET KE KM MG MU MW MZ RE RW SC SO TZ UG YT ZM ZW )],
27 '015' => [qw( DZ EA EG EH IC LY MA SD SS TN )],
28 '017' => [qw( AO CD CF CG CM GA GQ ST TD ZR )],
29 '018' => [qw( BW LS NA SZ ZA )],
30 '021' => [qw( BM CA GL PM US )],
31 '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 )],
32 '030' => [qw( CN HK JP KP KR MN MO TW )],
33 '034' => [qw( AF BD BT IN IR LK MV NP PK )],
34 '035' => [qw( BN BU ID KH LA MM MY PH SG TH TL TP VN )],
35 '039' => [qw( AD AL BA CS ES GI GR HR IT ME MK MT PT RS SI SM VA XK YU )],
36 '053' => [qw( AU NF NZ )],
37 '054' => [qw( FJ NC PG SB VU )],
38 '057' => [qw( FM GU KI MH MP NR PW )],
39 '061' => [qw( AS CK NU PF PN TK TO TV WF WS )],
40 '143' => [qw( KG KZ TJ TM UZ )],
41 '145' => [qw( AE AM AZ BH CY GE IL IQ JO KW LB NT OM PS QA SA SY TR YD YE )],
42 '151' => [qw( BG BY CZ HU MD PL RO RU SK SU UA )],
43 '154' => [qw( AX DK EE FI FO GB GG IE IM IS JE LT LV NO SE SJ )],
44 '155' => [qw( AT BE CH DD DE FR FX LI LU MC NL )],
45 '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 )],
46 'QO' => [qw( AC AQ BV CC CP CX DG GS HM IO TA TF UM )],
47 );
48  
49 # codes excluded from country list due to being deprecated or grouping container
50 2 my %noncountries = map { $_ => 1 } qw(
51 AN BU CS DD FX NT SU TP YD YU ZR
52 EU QO
53 );
54  
55 # deprecated aliases
56 2 my %alias_of = (
57 QU => 'EU',
58 UK => 'GB',
59 );
60  
61 sub coerce_region {
62 my ($region) = @_;
63  
64 return sprintf('%03d', $region)
65 if looks_like_number $region;
66  
67 return $alias_of{uc $region}
68 || uc $region;
69 }
70  
71 sub coerce_regions {
72 my ($regions) = @_;
73  
74 return [
75 map { coerce_region($_) }
76 ref $regions eq 'ARRAY' ? @$regions : $regions
77 ];
78 }
79  
80 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 has _includes => (
83 is => 'ro',
84 12 ▶ coerce => sub { coerce_regions(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 2 init_arg => 'include',
87 );
88  
89 has _excludes => (
90 is => 'ro',
91 12 ▶ coerce => sub { coerce_regions(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 11 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 init_arg => 'exclude',
94 );
95  
96 has _children => (
97 is => 'lazy',
98 builder => sub {
99 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
100 11 my $build_children;
101  
102 $build_children = sub { map {
103 72 ▶ $_, exists $children_of{$_}
Subroutine __ANON__: called CC=2 SCAR=6.9
104 596 ▶ ? $build_children->(@{$children_of{$_}})
Branch: 1 branch
BranchTrueFalseTotal
exists $children_of{$_} ? : 50 546 596
105 : ()
106 11 } @_ };
107  
108 29 my %excludes = map { $_ => 1 }
109 11 $build_children->(@{$self->_excludes});
110  
111 511 my %children = map { $_ => 1 }
112 567 grep { !exists $excludes{$_} }
113 11 $build_children->(@{$self->_includes});
114  
115 11 weaken $build_children;
116 11 return \%children;
117 },
118 2 );
119  
120 has _parents => (
121 is => 'lazy',
122 builder => sub {
123 7 ▶ my @regions = @{shift->_includes};
Subroutine _build__parents: called CC=1 SCAR=0.0
124 7 my ($build_parents, %count);
125  
126 $build_parents = sub { map {
127 35 ▶ my $region = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
128 $region, $build_parents->(grep {
129 28 ▶ any { $_ eq $region } @{$children_of{$_}}
Subroutine __ANON__: called CC=1 SCAR=0.0
130 } keys %children_of);
131 7 } @_ };
132  
133 16 my %parents = map { $_ => 1 }
134 7 grep { ++$count{$_} == @regions }
135 $build_parents->(@regions);
136  
137 7 weaken $build_parents;
138 7 return \%parents;
139 },
140 2 );
141  
142 has _countries => (
143 is => 'lazy',
144 builder => sub { [
145 sort
146 355 ▶ grep { /^[A-Z]{2}$/ && !exists $noncountries{$_} }
Branch: 1 branch
BranchTrueFalseTotal
/^[A-Z]{2}$/ and not exists $noncountries{$_} 316 39 355
147 10 ▶ keys %{shift->_children}
Subroutine _build__countries: called CC=2 SCAR=6.9
148 ] },
149 2 );
150  
151 sub BUILDARGS {
152 12 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=5 SCAR=16.1
Pod undocumented
153  
154 # the `include` key is optional for the first argument
155 12 ▶ my %args = @args == 1 && ref $args[0] eq 'HASH' ? %{$args[0]} :
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 1 11 12
@args % 2 ? : 1 10 11
Condition100%3/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
1011
MC/DC100%2/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
156 @args % 2 ? (include => @args) :
157 @args ;
158  
159 12 ▶ if (exists $args{region}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"region"}) 1 11 12
160 1 carp 'Argument "region" is deprecated; use "include" instead';
161 1 $args{include} = delete $args{region};
162 }
163  
164 12 return \%args;
165 }
166  
167 sub contains {
168 37 ▶ my ($self, $region) = @_;
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
169 37 return exists $self->_children->{ coerce_region($region) };
170 }
171  
172 sub is_within {
173 17 ▶ my ($self, $region) = @_;
Subroutine is_within: called CC=1 SCAR=0.0
Pod documented
174 17 return exists $self->_parents->{ coerce_region($region) };
175 }
176  
177 sub countries {
178 10 ▶ my ($self) = @_;
Subroutine countries: called CC=1 SCAR=0.0
Pod documented
179 10 return @{$self->_countries};
180 }
181  
182 2 1;
183  
184 __END__
« Build ↑ Summary blib/lib/Geo/Region/Constant.pm »