Country.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 93.1% 81 / 87 CC 6 SCAR 3.6
CC
6
Cov
94%
__ANON__
2.1
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
↑ Summary Country/Fast.pm »
1 package MooseX::Types::Locale::Country;
2  
3  
4 # ****************************************************************
5 # general dependency(-ies)
6 # ****************************************************************
7  
8 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # MooseX::Types turns strict/warnings pragmas on,
10 # however, kwalitee scorer can not detect such mechanism.
11 # (Perl::Critic can it, with equivalent_modules parameter)
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Locale::Country;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MooseX::Types::Moose qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 Str
18 Int
19 1 );
20 1 ▶use MooseX::Types (
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 -declare => [qw(
22 CountryCode
23 Alpha2Country
24 Alpha3Country
25 NumericCountry
26 CountryName
27 )],
28 1 );
29  
30  
31 # ****************************************************************
32 # namespace clearer
33 # ****************************************************************
34  
35 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37  
38 # ****************************************************************
39 # public class variable(s)
40 # ****************************************************************
41  
42 1 our $VERSION = "0.05";
43  
44  
45 # ****************************************************************
46 # private class variable(s)
47 # ****************************************************************
48  
49 # Because code2country($_) cannot coerce 'JP' to 'jp'.
50 1 my %alpha2;
51 1 @alpha2{ map { uc } all_country_codes(LOCALE_CODE_ALPHA_2) } = ();
52  
53 1 my %alpha3;
54 1 @alpha3{ map { uc } all_country_codes(LOCALE_CODE_ALPHA_3) } = ();
55  
56 # Because country2code($_) cannot coerce 'japanese' to 'Japanese'.
57 1 my %name;
58 1 @name{ all_country_names() } = ();
59  
60  
61 # ****************************************************************
62 # subtype(s) and coercion(s)
63 # ****************************************************************
64  
65 # ----------------------------------------------------------------
66 # alpha-2 country code as defined in ISO 3166-1
67 # ----------------------------------------------------------------
68 1 foreach my $subtype (CountryCode, Alpha2Country) {
69 subtype $subtype,
70 as Str,
71 12 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 12 exists $alpha2{$_};
73 },
74 4 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
75 4 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 2 2 4
76 . 'because specified country code does not exist '
77 . 'in ISO 3166-1 alpha-2',
78 defined $_ ? $_ : q{};
79 2 };
80  
81 coerce $subtype,
82 from Str,
83 4 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 # - Converts 'us' into 'US'.
85 # - ISO 3166 recommended upper-case.
86 4 return uc $_;
87 2 };
88 }
89  
90 # ----------------------------------------------------------------
91 # alpha-3 country code as defined in ISO 3166-1
92 # ----------------------------------------------------------------
93 subtype Alpha3Country,
94 as Str,
95 6 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 6 exists $alpha3{$_};
97 },
98 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
99 2 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
100 . 'because specified country code does not exist '
101 . 'in ISO 3166-1 alpha-3',
102 defined $_ ? $_ : q{};
103 1 };
104  
105 coerce Alpha3Country,
106 from Str,
107 2 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 # - Converts 'us' into 'US'.
109 # - ISO 3166 recommended upper-case.
110 2 return uc $_;
111 1 };
112  
113 # ----------------------------------------------------------------
114 # numeric country code as defined in ISO 3166-1
115 # ----------------------------------------------------------------
116 subtype NumericCountry,
117 as Str,
118 3 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 3 code2country($_, LOCALE_CODE_NUMERIC);
120 },
121 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
122 2 ▶ sprintf 'Validation failed for code failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
123 . 'because specified country code does not exist '
124 . 'in ISO 3166-1 numeric',
125 defined $_ ? $_ : q{};
126 1 };
127  
128 # ----------------------------------------------------------------
129 # country name as defined in ISO 3166-1
130 # ----------------------------------------------------------------
131 subtype CountryName,
132 as Str,
133 6 ▶ where {
Subroutine __ANON__: called CC=1 SCAR=0.0
134 6 exists $name{$_};
135 },
136 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 2 ▶ sprintf 'Validation failed for name failed with value (%s) '
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 1 2
138 . 'because specified country name does not exist '
139 . 'in ISO 3166-1',
140 defined $_ ? $_ : q{};
141 1 };
142  
143 coerce CountryName,
144 from Str,
145 2 ▶ via {
Subroutine __ANON__: called CC=2 SCAR=7.2
146 # - Converts 'eNgLiSh' into 'English'.
147 # - Cannot use 'ucfirst lc', because there is 'Rhaeto-Romance'.
148 # - In case of invalid name, returns original $_
149 # to use it in exception message.
150 2 ▶ return code2country( country2code($_) ) || $_;
Condition66%2/3
code2country(country2code($_)) || $_
l!l&&r!l&&!r
110
MC/DC0%0/2
code2country(country2code($_)) || $_
code2country(country2code($_)) $_
Truth table66%2/3
A || B
A= code2country(country2code($_))B= $_
ABresult
1X1
011
000
151 1 };
152  
153  
154 # ****************************************************************
155 # optionally add Getopt option type
156 # ****************************************************************
157  
158 1 eval { require MooseX::Getopt; };
159 1 ▶if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 1 1
160 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
161 0 for (CountryCode, Alpha2Country, Alpha3Country, CountryName);
162 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=i', )
163 0 for (NumericCountry);
164 }
165  
166  
167 # ****************************************************************
168 # return true
169 # ****************************************************************
170  
171 1 1;
172 __END__
↑ Summary Country/Fast.pm »