blib/lib/Mojolicious/Plugin/CountryDropDown.pm

filter: Statementstmt 90.6% 213 / 235 Branchbran 77.0% 77 / 100 Conditioncond 60.9% 50 / 82 MC/DCmcdc 34.0% 17 / 50 Subroutinesub 100% 27 / 27 Podpod 100% 1 / 1 totaltotal 77.7% 385 / 495 CC 88 SCAR 21.1
CC
88
Cov
82%
_build_country_list
16.6
__ANON__
15.0
_get_language
12.5
CRAP 8.2
↑ Summary
1  
2 package Mojolicious::Plugin::CountryDropDown;
3  
4 # ABSTRACT: Provide a localizable dropdown where users can select a country
5  
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Locale::Country::Multilingual { use_io_layer => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use List::MoreUtils qw( zip pairwise );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $VERSION = 0.06;
16 11 $VERSION = eval $VERSION; ## no critic ProhibitStringyEval
17  
18 10 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
19 10 my ( $plugin, $app, $opts ) = @_;
20  
21 10 ▶ my $collate = Unicode::Collate->new()
Branch: 1 branch
BranchTrueFalseTotal
my $collate = "Unicode::Collate"->new or confess("Unsatisfied dependency: Unicode::Collate") 10 0 10
22 or confess 'Unsatisfied dependency: Unicode::Collate';
23 10 ▶ my $lcm = Locale::Country::Multilingual->new()
Branch: 1 branch
BranchTrueFalseTotal
my $lcm = "Locale::Country::Multilingual"->new or confess("Unsatisfied dependency: Locale::Country::Multilingual") 10 0 10
24 or confess 'Unsatisfied dependency: Locale::Country::Multilingual';
25  
26 10 $plugin->{_collate} = $collate;
27 10 $plugin->{_lcm} = $lcm;
28  
29 10 $plugin->_build_conf( $app, $opts );
30  
31 $app->helper(
32 14 ▶ 'country_select_field' => sub {
Subroutine __ANON__: called CC=15 SCAR=27.1
33 14 my ( $c, $opts ) = @_;
34  
35 14 ▶ my $selected = $opts->{select} || $plugin->{_conf}->{select} || '';
Condition100%5/5
$opts->{'select'} || $plugin->{'_conf'}{'select'} || ''
l!l
59
$opts->{"select"} or $plugin->{"_conf"}{"select"}
l!l&&r!l&&!r
239
MC/DC100%2/2
$opts->{'select'} || $plugin->{'_conf'}{'select'} || ''
$opts->{"select"} $plugin->{"_conf"}{"select"}
Truth table100%3/3
A or B
A= $opts->{"select"}B= $plugin->{"_conf"}{"select"}
ABresult
1X1
011
000
36 14 my %countries = %{ $plugin->_build_country_list( $c, $opts ) };
37  
38 14 my @preferred = ();
39 14 ▶ if ( exists $opts->{prefer} and ( ref $opts->{prefer} eq ref [] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'prefer'} and ref $opts->{'prefer'} eq ref []) { } 2 12 14
Condition66%2/3
exists $opts->{"prefer"} and ref $opts->{"prefer"} eq ref []
!ll&&!rl&&r
1202
MC/DC50%1/2
exists $opts->{"prefer"} and ref $opts->{"prefer"} eq ref []
exists $opts->{"prefer"} ref $opts->{"prefer"} eq ref []
Truth table66%2/3
A and B
A= exists $opts->{"prefer"}B= ref $opts->{"prefer"} eq ref []
ABresult
0X0
100
111
40 2 @preferred = map { uc($_) } @{ $opts->{prefer} };
41 }
42 else {
43 12 ▶ @preferred = @{ $plugin->{_conf}->{prefer} || [] };
Condition100%2/2
$plugin->{'_conf'}{'prefer'} || []
l!l
210
MC/DC100%1/1
$plugin->{'_conf'}{'prefer'} || []
$plugin->{'_conf'}{'prefer'}
Truth table100%2/2
A
A= $plugin->{'_conf'}{'prefer'}
Aresult
11
00
44 }
45  
46 14 my @vals = values(%countries);
47 14 my @keys = keys(%countries);
48 14 my %countries_by_name = zip @vals, @keys;
49  
50 # simply using "sort" isn't appropriate...
51 14 my @sorted_names = $plugin->{_collate}->sort( keys %countries_by_name );
52 14 my @sorted_codes = @countries_by_name{@sorted_names};
53  
54 14 my @preferred_options = ();
55 14 ▶ if (@preferred) {
Branch: 1 branch
BranchTrueFalseTotal
if (@preferred) 4 10 14
56 4 foreach my $code (@preferred) {
57 12 my @opt = ( $countries{$code}, $code );
58 12 ▶ if ( $code eq $selected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $selected) 3 9 12
59 3 push @opt, selected => 'selected';
60 3 $selected = '';
61 }
62 12 push @preferred_options, [@opt];
63 }
64 4 push @preferred_options, [ '-----', '', disabled => 'disabled' ];
65 }
66  
67 my @options = (
68 @preferred_options,
69 3498 ▶ pairwise {
Subroutine __ANON__: called CC=2 SCAR=6.9
70  
71 # $a = $names->[$n], $b = $codes->[$n]
72 3498 my $option = [ $a, $b ];
73 3498 ▶ push @$option, selected => "selected" if $b eq $selected;
Branch: 1 branch
BranchTrueFalseTotal
if $b eq $selected 2 3496 3498
74 3498 $option;
75 }
76 14 @sorted_names,
77 @sorted_codes
78 );
79  
80 # html code gets generated by Mojolicious
81 14 ▶ my %attr = %{ $opts->{html_attr} || $plugin->{_conf}->{html_attr} || {} };
Condition60%3/5
$opts->{'html_attr'} || $plugin->{'_conf'}{'html_attr'} || {}
l!l
140
$opts->{"html_attr"} or $plugin->{"_conf"}{"html_attr"}
l!l&&r!l&&!r
3110
MC/DC0%0/2
$opts->{'html_attr'} || $plugin->{'_conf'}{'html_attr'} || {}
$opts->{"html_attr"} $plugin->{"_conf"}{"html_attr"}
Truth table66%2/3
A or B
A= $opts->{"html_attr"}B= $plugin->{"_conf"}{"html_attr"}
ABresult
1X1
011
000
82 14 ▶ my $name = delete( $attr{name} ) || 'country';
Condition100%2/2
delete $attr{'name'} || 'country'
l!l
122
MC/DC100%1/1
delete $attr{'name'} || 'country'
delete $attr{'name'}
Truth table100%2/2
A
A= delete $attr{'name'}
Aresult
11
00
83  
84 14 ▶ if ( !exists $attr{id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $attr{'id'}) { } 11 3 14
elsif (exists $attr{'id'} and not defined $attr{'id'}) { } 2 1 3
85 11 $attr{id} = $name;
86 }
87 ▶ elsif ( exists $attr{id} and not defined $attr{id} ) {
Condition66%2/3
exists $attr{"id"} and not defined $attr{"id"}
!ll&&!rl&&r
012
MC/DC50%1/2
exists $attr{"id"} and not defined $attr{"id"}
exists $attr{"id"} not defined $attr{"id"}
Truth table66%2/3
A and not(B)
A= exists $attr{"id"}B= not defined $attr{"id"}
ABresult
0X0
100
111
88 2 delete $attr{id};
89 }
90  
91 14 return $c->select_field( $name, \@options, %attr );
92 }
93 10 );
94  
95 10 ▶ $app->helper( 'country_drop_down' => sub { return shift->country_select_field(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 $app->helper(
98 7 ▶ 'csf_country_list' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
99 7 my $c = shift;
100 7 ▶ my $opts = shift // {};
Condition100%2/2
shift() // {}
l!l
25
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
101 7 return $plugin->_build_country_list( $c, $opts );
102 }
103 10 );
104  
105 $app->helper(
106 20 ▶ 'csf_conf' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
107 20 my ( $c, $new ) = @_;
108  
109 20 ▶ unless ( defined $new ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 1 19 20
110 1 return $plugin->{_conf};
111 }
112  
113 19 ▶ if ( ref $new eq ref {} and !%{$new} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new eq ref {} and not %{$new;}) 2 17 19
Condition66%2/3
ref $new eq ref {} and not %{$new;}
!ll&&!rl&&r
0172
MC/DC50%1/2
ref $new eq ref {} and not %{$new;}
ref $new eq ref {} not %{$new;}
Truth table66%2/3
A and not(B)
A= ref $new eq ref {}B= not %{$new;}
ABresult
0X0
100
111
114 2 delete $plugin->{_conf};
115 }
116  
117 19 $plugin->_build_conf( $c, $new );
118 19 return $plugin->{_conf};
119 }
120 10 );
121  
122 $app->helper(
123 12 ▶ 'code2country' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
124 12 my ( $c, $code ) = ( shift, shift );
125  
126 12 ▶ unless ( defined $code and $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code and $code) 1 11 12
Condition66%2/3
defined $code and $code
!ll&&!rl&&r
0111
MC/DC50%1/2
defined $code and $code
defined $code $code
Truth table66%2/3
A and B
A= defined $codeB= $code
ABresult
0X0
100
111
127 1 $c->app->log->warn('code2country() called without a code');
128 1 return;
129 }
130  
131 11 my $lang = $plugin->_get_language( $c, shift );
132  
133 11 return $plugin->{_lcm}->code2country( $code, $lang );
134 }
135 10 );
136  
137 $app->helper(
138 12 ▶ 'country2code' => sub {
Subroutine __ANON__: called CC=4 SCAR=16.1
139 12 my ( $c, $country, $lang, $codeset ) = @_;
140  
141 12 ▶ unless ( defined $country and $country ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $country and $country) 0 12 12
Condition33%1/3
defined $country and $country
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $country and $country
defined $country $country
Truth table33%1/3
A and B
A= defined $countryB= $country
ABresult
0X0
100
111
142 0 $c->app->log->warn('country2code() called without a country name');
143 0 return;
144 }
145  
146 12 $lang = $plugin->_get_language( $app, $lang );
147 12 ▶ $codeset = $plugin->_check_codeset( $app, $codeset ) // $plugin->{_conf}->{codeset};
Condition66%2/3
$plugin->_check_codeset($app, $codeset) // $plugin->{"_conf"}{"codeset"}
l!l&&r!l&&!r
660
MC/DC0%0/2
$plugin->_check_codeset($app, $codeset) // $plugin->{"_conf"}{"codeset"}
$plugin->_check_codeset($app, $codeset) $plugin->{"_conf"}{"codeset"}
Truth table66%2/3
A // B
A= $plugin->_check_codeset($app, $codeset)B= $plugin->{"_conf"}{"codeset"}
ABresult
1X1
011
000
148  
149 12 return $plugin->{_lcm}->country2code( $country, $codeset, $lang );
150 }
151 10 );
152 10 return;
153 } ## end sub register
154  
155 # Determine the most appropriate language to use using a fallback hierarchy
156  
157 44 ▶sub _get_language {
Subroutine _get_language: called CC=8 SCAR=25.3
158 44 my ( $self, $c, $param ) = @_;
159  
160 # param considered first
161 44 ▶ if ( defined $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) 16 28 44
162 16 ▶ if ( $self->_check_lng_avail($param) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_lng_avail($param)) 16 0 16
163 16 return $param;
164 }
165 }
166  
167 # explicitly configured languages considered next
168 28 ▶ if ( exists $self->{_conf}->{language} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_conf"}{"language"}) 11 17 28
169 11 return $self->{_conf}->{language};
170 }
171  
172 # fallback: language determined by I18N plugin
173 17 ▶ if ( defined $c->stash->{i18n}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->stash->{"i18n"} and my $l = $c->stash->{"i18n"}->languages) 0 17 17
Condition33%1/3
defined $c->stash->{"i18n"} and my $l = $c->stash->{"i18n"}->languages
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $c->stash->{"i18n"} and my $l = $c->stash->{"i18n"}->languages
defined $c->stash->{"i18n"} my $l = $c->stash->{"i18n"}->languages
Truth table33%1/3
A and B
A= defined $c->stash->{"i18n"}B= my $l = $c->stash->{"i18n"}->languages
ABresult
0X0
100
111
174 && ( my $l = $c->stash->{i18n}->languages() ) )
175 {
176 0 ▶ if ( $self->_check_lng_avail($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_lng_avail($l)) 0 0 0
177 0 return $l;
178 }
179  
180 0 $c->app->log->debug( 'The language determined by the I18N plugin is not available; using fallback "en"' );
181 }
182  
183 # fallback: default = en
184 17 ▶ if ( $self->_check_lng_avail('en') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_lng_avail("en")) 17 0 17
185 17 return 'en';
186 }
187  
188 # 'en' not available? something“s borken...
189 0 $c->app->log->error( "Loading language 'en' failed! Broken installation of Locale::Country::Multilingual?" );
190 0 confess "Default language 'en' not available!";
191 } ## end sub _get_language
192  
193 44 ▶sub _check_lng_avail {
Subroutine _check_lng_avail: called CC=7 SCAR=19.6
194 44 my ( $self, $l ) = @_;
195  
196 44 ▶ my %lng_avail = %{ $self->{_lng_avail} || {} };
Condition100%2/2
$self->{'_lng_avail'} || {}
l!l
3410
MC/DC100%1/1
$self->{'_lng_avail'} || {}
$self->{'_lng_avail'}
Truth table100%2/2
A
A= $self->{'_lng_avail'}
Aresult
11
00
197 44 ▶ return 1 if defined $lng_avail{$l} and $lng_avail{$l};
Branch: 1 branch
BranchTrueFalseTotal
if defined $lng_avail{$l} and $lng_avail{$l} 22 22 44
Condition66%2/3
defined $lng_avail{$l} and $lng_avail{$l}
!ll&&!rl&&r
22022
MC/DC50%1/2
defined $lng_avail{$l} and $lng_avail{$l}
defined $lng_avail{$l} $lng_avail{$l}
Truth table66%2/3
A and B
A= defined $lng_avail{$l}B= $lng_avail{$l}
ABresult
0X0
100
111
198 22 ▶ return 0 if defined $lng_avail{$l};
Branch: 1 branch
BranchTrueFalseTotal
if defined $lng_avail{$l} 0 22 22
199  
200 # check if L::C::M supports this language
201 # results are cached
202 22 my $loaded = $self->{_lcm}->assert_lang($l);
203  
204 22 ▶ if ( defined $loaded and lc($loaded) eq lc($l) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loaded and lc $loaded eq lc $l) { } 21 1 22
Condition66%2/3
defined $loaded and lc $loaded eq lc $l
!ll&&!rl&&r
1021
MC/DC50%1/2
defined $loaded and lc $loaded eq lc $l
defined $loaded lc $loaded eq lc $l
Truth table66%2/3
A and B
A= defined $loadedB= lc $loaded eq lc $l
ABresult
0X0
100
111
205 21 $self->{_lng_avail}->{$l} = 1;
206 21 return 1;
207 }
208 else {
209 1 $self->{_lng_avail}->{$l} = 0;
210 1 return 0;
211 }
212 }
213  
214 29 ▶sub _build_conf {
Subroutine _build_conf: called CC=5 SCAR=16.1
215 29 my ( $self, $c, $opts ) = @_;
216  
217 29 ▶ unless ( defined $self->{_conf} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_conf"}) 12 17 29
218 # populate basic defaults
219 12 $self->{_conf} = { html_attr => { name => 'country' }, codeset => 'LOCALE_CODE_ALPHA_2', };
220 }
221  
222 29 ▶ my %conf = %{ $opts || {} };
Condition50%1/2
$opts || {}
l!l
290
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
223 29 foreach my $p (qw/ select language names prefer exclude html_attr codeset /) {
224 203 ▶ if ( exists $conf{$p} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{$p}) 32 171 203
225 32 my $method = '_' . $p;
226 32 $self->$method( $c, $conf{$p} );
227 }
228 }
229  
230 29 return;
231 }
232  
233 21 ▶sub _build_country_list {
Subroutine _build_country_list: called CC=13 SCAR=28.1
234 21 my ( $self, $c ) = ( shift, shift );
235 21 ▶ my %opts = %{ shift || {} };
Condition50%1/2
shift() || {}
l!l
210
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
236  
237 21 $self->{_lcm}->set_lang( $self->_get_language( $c, $opts{language} ) );
238  
239 # get the codes from the right codeset and the country names in the language set above
240 my $codeset = $self->_check_codeset( $c, $opts{codeset} )
241 21 ▶ || $self->{_conf}->{codeset};
Condition33%1/3
$self->_check_codeset($c, $opts{'codeset'}) || $self->{'_conf'}{'codeset'}
l!l&&r!l&&!r
0210
MC/DC0%0/2
$self->_check_codeset($c, $opts{'codeset'}) || $self->{'_conf'}{'codeset'}
$self->_check_codeset($c, $opts{'codeset'}) $self->{'_conf'}{'codeset'}
Truth table33%1/3
A || B
A= $self->_check_codeset($c, $opts{'codeset'})B= $self->{'_conf'}{'codeset'}
ABresult
1X1
011
000
242 21 my $list = {};
243  
244 21 ▶ if ( $codeset eq 'LOCALE_CODE_ALPHA_2' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($codeset eq 'LOCALE_CODE_ALPHA_2') { } 19 2 21
245 19 my @codes = $self->{_lcm}->all_country_codes($codeset);
246 19 my @names = $self->{_lcm}->all_country_names();
247 19 %{$list} = zip @codes, @names;
248 }
249 else {
250 # performance killer, but I don“t see another solution
251 2 foreach my $code ( $self->{_lcm}->all_country_codes($codeset) ) {
252 498 $list->{$code} = $self->{_lcm}->code2country($code);
253 }
254 }
255  
256 # remove excluded countries
257 21 my @excl = ();
258 21 ▶ if ( defined $opts{exclude} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'exclude'}) { } 1 20 21
elsif (defined $self->{'_conf'}{'exclude'}) { } 0 20 20
259 1 @excl = @{ $opts{exclude} };
260 }
261 elsif ( defined $self->{_conf}->{exclude} ) {
262 0 @excl = @{ $self->{_conf}->{exclude} };
263 }
264 21 for my $code (@excl) {
265 1 $code = uc($code);
266 1 delete $list->{$code};
267 }
268  
269 # replace official country names with app-specific names
270 21 ▶ if ( defined $opts{names} and ( ref $opts{names} eq ref {} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'names'} and ref $opts{'names'} eq ref {}) { } 0 21 21
elsif (exists $self->{'_conf'}{'names'}) { } 4 17 21
Condition33%1/3
defined $opts{"names"} and ref $opts{"names"} eq ref {}
!ll&&!rl&&r
2100
MC/DC0%0/2
defined $opts{"names"} and ref $opts{"names"} eq ref {}
defined $opts{"names"} ref $opts{"names"} eq ref {}
Truth table33%1/3
A and B
A= defined $opts{"names"}B= ref $opts{"names"} eq ref {}
ABresult
0X0
100
111
271 0 ▶ while ( my ( $k, $v ) = each %{ $opts{names} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$opts{"names"};}) 0 0 0
272 0 $k = uc($k);
273 0 $list->{$k} = $v;
274 }
275 }
276 elsif ( exists $self->{_conf}->{names} ) {
277 4 ▶ while ( my ( $k, $v ) = each %{ $self->{_conf}->{names} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_conf"}{"names"};}) 12 4 16
278 12 $k = uc($k);
279 12 $list->{$k} = $v;
280 }
281 }
282  
283 # return a hash of country names indexed by code
284 21 return $list;
285 } ## end sub _build_country_list
286  
287 39 ▶sub _check_codeset {
Subroutine _check_codeset: called CC=5 SCAR=16.1
288 39 my ( $self, $c, $candidate ) = @_;
289  
290 39 ▶ return unless defined $candidate and $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $candidate and $candidate 27 12 39
Condition66%2/3
defined $candidate and $candidate
!ll&&!rl&&r
27012
MC/DC50%1/2
defined $candidate and $candidate
defined $candidate $candidate
Truth table66%2/3
A and B
A= defined $candidateB= $candidate
ABresult
0X0
100
111
291 12 $candidate = uc($candidate);
292  
293 12 ▶ unless ( index( $candidate, "LOCALE_CODE_" ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (index($candidate, "LOCALE_CODE_") == 0) 8 4 12
294 8 $candidate = "LOCALE_CODE_" . $candidate;
295 }
296  
297 12 ▶ if ( $candidate =~ /\ALOCALE_CODE_(?:ALPHA_[23]|NUMERIC)\Z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($candidate =~ /\ALOCALE_CODE_(?:ALPHA_[23]|NUMERIC)\Z/u) 11 1 12
298 11 return $candidate;
299 }
300  
301 1 $c->app->log->warn('Invalid value specified for codeset');
302  
303 1 return;
304 }
305  
306 # Following here are helper functions for (re-)setting various config elements
307  
308 2 ▶sub _select {
Subroutine _select: called CC=3 SCAR=12.3
309 2 my ( $self, $c, $s ) = @_;
310  
311 2 ▶ if ( defined $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 2 0 2
312 2 ▶ if ( ref $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s) { } 0 2 2
313 0 $c->app->log->warn('Please provide a scalar value');
314 }
315 else {
316 2 $self->{_conf}->{select} = uc($s);
317 }
318 }
319 else {
320 0 delete $self->{_conf}->{select};
321 }
322  
323 2 return;
324 }
325  
326 7 ▶sub _codeset {
Subroutine _codeset: called CC=3 SCAR=11.0
327 7 my ( $self, $c, $code ) = @_;
328  
329 7 ▶ if ( defined $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) { } 6 1 7
330 6 ▶ if ( my $val = $self->_check_codeset( $c, $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->_check_codeset($c, $code)) 5 1 6
331 5 $self->{_conf}->{codeset} = $val;
332 }
333 }
334 else {
335 1 $self->{_conf}->{codeset} = 'LOCALE_CODE_ALPHA_2';
336 }
337  
338 7 return;
339 }
340  
341 13 ▶sub _language {
Subroutine _language: called CC=4 SCAR=13.9
342 13 my ( $self, $c, $lang ) = @_;
343  
344 13 ▶ if ( defined $lang and $lang ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang and $lang) { } 11 2 13
Condition66%2/3
defined $lang and $lang
!ll&&!rl&&r
2011
MC/DC50%1/2
defined $lang and $lang
defined $lang $lang
Truth table66%2/3
A and B
A= defined $langB= $lang
ABresult
0X0
100
111
345 11 ▶ if ( $self->_check_lng_avail($lang) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_lng_avail($lang)) { } 10 1 11
346 10 $self->{_conf}->{language} = $lang;
347 }
348 else {
349 1 $c->app->log->warn("Language '$lang' is not supported!");
350 1 delete $self->{_conf}->{language};
351 }
352 }
353 else {
354 2 delete $self->{_conf}->{language};
355 }
356  
357 13 return;
358 }
359  
360 3 ▶sub _prefer {
Subroutine _prefer: called CC=7 SCAR=20.8
361 3 my ( $self, $c, $codes ) = @_;
362  
363 3 ▶ if ( defined $codes and $codes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $codes and $codes) { } 2 1 3
Condition66%2/3
defined $codes and $codes
!ll&&!rl&&r
102
MC/DC50%1/2
defined $codes and $codes
defined $codes $codes
Truth table66%2/3
A and B
A= defined $codesB= $codes
ABresult
0X0
100
111
364 2 ▶ if ( ref $codes and not( ref $codes eq ref [] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codes and not ref $codes eq ref []) { } 0 2 2
Condition33%1/3
ref $codes and not ref $codes eq ref []
!ll&&!rl&&r
020
MC/DC0%0/2
ref $codes and not ref $codes eq ref []
ref $codes not ref $codes eq ref []
Truth table33%1/3
A and not(B)
A= ref $codesB= not ref $codes eq ref []
ABresult
0X0
100
111
365 0 $c->app->log->warn( 'Either provide scalar value or array ref with name(s) of preferred countries' );
366 }
367 else {
368 2 ▶ $codes = [$codes] unless ref $codes;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $codes 0 2 2
369 2 $_ = uc($_) foreach ( @{$codes} );
370 2 $self->{_conf}->{prefer} = $codes;
371 }
372 }
373 else {
374 1 delete $self->{_conf}->{prefer};
375 }
376  
377 3 return;
378 }
379  
380 2 ▶sub _exclude {
Subroutine _exclude: called CC=7 SCAR=20.8
381 2 my ( $self, $c, $codes ) = @_;
382  
383 2 ▶ if ( defined $codes and $codes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $codes and $codes) { } 1 1 2
Condition66%2/3
defined $codes and $codes
!ll&&!rl&&r
101
MC/DC50%1/2
defined $codes and $codes
defined $codes $codes
Truth table66%2/3
A and B
A= defined $codesB= $codes
ABresult
0X0
100
111
384 1 ▶ if ( ref $codes and not( ref $codes eq ref [] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $codes and not ref $codes eq ref []) { } 0 1 1
Condition33%1/3
ref $codes and not ref $codes eq ref []
!ll&&!rl&&r
010
MC/DC0%0/2
ref $codes and not ref $codes eq ref []
ref $codes not ref $codes eq ref []
Truth table33%1/3
A and not(B)
A= ref $codesB= not ref $codes eq ref []
ABresult
0X0
100
111
385 0 $c->app->log->warn( 'Either provide scalar value or array ref with name(s) of excluded countries' );
386 }
387 else {
388 1 ▶ $codes = [$codes] unless ref $codes;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $codes 0 1 1
389 1 $_ = uc($_) foreach ( @{$codes} );
390 1 $self->{_conf}->{exclude} = $codes;
391 }
392 }
393 else {
394 1 delete $self->{_conf}->{exclude};
395 }
396  
397 2 return;
398 }
399  
400 2 ▶sub _names {
Subroutine _names: called CC=5 SCAR=18.1
401 2 my ( $self, $c, $names ) = @_;
402  
403 2 ▶ if ( defined $names and $names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $names and $names) { } 2 0 2
Condition33%1/3
defined $names and $names
!ll&&!rl&&r
002
MC/DC0%0/2
defined $names and $names
defined $names $names
Truth table33%1/3
A and B
A= defined $namesB= $names
ABresult
0X0
100
111
404 2 ▶ if ( not ref $names eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $names eq ref {}) { } 0 2 2
405 0 $c->app->log->warn( 'Please provide a hash ref with a mapping of country ids to names!' );
406 }
407 else {
408 2 foreach ( keys %{$names} ) {
409 5 $names->{ uc($_) } = delete $names->{$_};
410 }
411 2 $self->{_conf}->{names} = $names;
412 }
413 }
414 else {
415 0 delete $self->{_conf}->{names};
416 }
417  
418 2 return;
419 }
420  
421 3 ▶sub _html_attr {
Subroutine _html_attr: called CC=6 SCAR=20.5
422 3 my ( $self, $c, $attr ) = @_;
423  
424 3 ▶ if ( defined $attr and $attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr and $attr) { } 3 0 3
Condition33%1/3
defined $attr and $attr
!ll&&!rl&&r
003
MC/DC0%0/2
defined $attr and $attr
defined $attr $attr
Truth table33%1/3
A and B
A= defined $attrB= $attr
ABresult
0X0
100
111
425 3 ▶ if ( not ref $attr eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $attr eq ref {}) { } 0 3 3
426 0 $c->app->log->warn('Please provide a hash ref with attribute names and values!');
427 }
428 else {
429 3 $self->{_conf}->{html_attr} = $attr;
430 3 ▶ unless ( defined $self->{_conf}->{html_attr}->{name}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_conf"}{"html_attr"}{"name"} and $self->{"_conf"}{"html_attr"}{"name"} =~ /\A\S+\Z/u) 1 2 3
Condition66%2/3
defined $self->{"_conf"}{"html_attr"}{"name"} and $self->{"_conf"}{"html_attr"}{"name"} =~ /\A\S+\Z/u
!ll&&!rl&&r
102
MC/DC50%1/2
defined $self->{"_conf"}{"html_attr"}{"name"} and $self->{"_conf"}{"html_attr"}{"name"} =~ /\A\S+\Z/u
defined $self->{"_conf"}{"html_attr"}{"name"} $self->{"_conf"}{"html_attr"}{"name"} =~ /\A\S+\Z/u
Truth table66%2/3
A and B
A= defined $self->{"_conf"}{"html_attr"}{"name"}B= $self->{"_conf"}{"html_attr"}{"name"} =~ /\A\S+\Z/u
ABresult
0X0
100
111
431 and $self->{_conf}->{html_attr}->{name} =~ /\A\S+\Z/ )
432 {
433 1 $self->{_conf}->{html_attr}->{name} = 'country';
434 }
435 }
436 }
437 else {
438 0 delete $self->{_conf}->{html_attr};
439  
440 # form field with no name makes no sense
441 # set default name "country"
442 0 $self->{_conf}->{html_attr} = { name => 'country' };
443 }
444  
445 3 return;
446 } ## end sub _html_attr
447  
448 11 1;
449  
450  
451  
452  
453  
454 =pod
455  
456 =head1 NAME
457  
458 Mojolicious::Plugin::CountryDropDown - Provide a localizable dropdown where users can select a country
459  
460 =head1 VERSION
461  
462 version 0.06
463  
464 =head1 SYNOPSIS
465  
466 use Mojolicious::Plugin::CountryDropDown;
467  
468 sub startup {
469 my $self = shift;
470  
471 $self->plugin('CountryDropDown');
472 ...
473  
474 In your controller:
475  
476 get '/' => sub {
477 my $self = shift;
478  
479 $self->csf_conf({ language => 'de' }); # this sets the default language
480 ...
481  
482 In your template (this time with TemplateToolkit syntax):
483  
484 [% h.country_select_field({ html_attr => { class => 'shiny', } }) %]
485  
486 =encoding utf-8
487  
488 =head1 NAME
489  
490 Mojolicious::Plugin::CountryDrowDown - use a localized dropdown ("select" field) to let
491 your users select countries in your HTML forms.
492  
493 =head1 VERSION
494  
495 Version 0.06
496  
497 =head1 WARNINGS
498  
499 Version 0.04 was the first public release and considered a beta release!
500  
501 Version 0.05_0x and later include some extensive API changes and are
502 generally incompatible with 0.04 - so please watch out when updating!
503  
504 =head1 CONFIGURATION
505  
506 You may pass a hash ref on plugin registration. The following keys are currently
507 recognized:
508  
509 =over 4
510  
511 =item language
512  
513 Language code (string). The language used for the country names.
514  
515 Valid values are those known to L<Locale::Country::Multilingual|Locale::Country::Multilingual>.
516 Invalid values will be silently ignored. See section "LANGUAGE FALLBACKS" below.
517  
518 =item select
519  
520 Country code (string). Sets the country to be selected by default.
521  
522 Valid values are those known to L<Locale::Country::Multilingual|Locale::Country::Multilingual>.
523 Invalid values (i.e. unknown codes) will be accepted but of course have no effect when generating
524 the form (unless you add artificial country names for these codes using the C<names> config key).
525 See section "CODESETS" below.
526  
527 =item prefer
528  
529 Array reference with a list of country codes which are put to the
530 top of the select field in the order in which they appear in this list.
531 A spacer option is added after them and before the following list of countries.
532 The countries specified here aren“t currently removed from the following list
533 so that they will appear twice within the select form field.
534 If one of these preferred countries is also "select"ed, the pre-selection will
535 happen in the prepended section of preferred countries.
536  
537 Valid values are country codes known to L<Locale::Country::Multilingual|Locale::Country::Multilingual>.
538 Invalid values will be silently ignored. See section "CODESETS" below.
539  
540 =item exclude
541  
542 Array reference with a list of country codes which are removed from the
543 list.
544  
545 Valid values are country codes known to L<Locale::Country::Multilingual|Locale::Country::Multilingual>.
546 Invalid values will be silently ignored. See section "CODESETS" below.
547  
548 =item html_attr
549  
550 Hash reference whose pairs will be used as HTML attributes within the opening
551 "select" tag. No validation is performed with regard to any HTML doctype!
552  
553 The attribute "name" will always be set for the "select" element. By default it's
554 value will be "country".
555  
556 The attribute "id" will also be set for the "select" element by default. The
557 default value is the currently configured value for the "name" attribute.
558 Unlike with the "name" attribute you can remove the "id" attribute by setting
559 the configured value to "undef".
560  
561 =item names
562  
563 Hash reference with pairs of country codes and associated names.
564 The names specified here will be used instead of the "official" names as provided by
565 L<Locale::Country::Multilingual|Locale::Country::Multilingual>. The replacement happens
566 without taking the currently used language into account!
567  
568 The hash keys have to be country codes from the appropriate codeset!
569 See section "CODESETS" below.
570  
571 On the other hand you can use this to inject extra "countries" but it's up to you to
572 provide unique codes and translate the names appropriately to the currently used
573 language.
574  
575 =item codeset
576  
577 Codeset id as used by L<Locale::Country::Multilingual|Locale::Country::Multilingual>.
578 Either C<LOCALE_CODE_ALPHA_2>, C<LOCALE_CODE_ALPHA_3> or C<LOCALE_CODE_NUMERIC>.
579 By default, 2-letter country codes (C<LOCALE_CODE_ALPHA_2>) are used.
580  
581 =back
582  
583 =head1 LANGUAGE FALLBACKS
584  
585 This module will use a fallback hierarchy to determine which language it has to use
586 for the country names.
587  
588 =over 4
589  
590 =item *
591  
592 Highest precedence lies with a parameter to the current function call.
593  
594 =item *
595  
596 Next the value from the configuration is used, if set.
597  
598 =item *
599  
600 If in use in the application the Mojolicious plugin C<I18N> is used next and asked for
601 the language it chose.
602  
603 =item *
604  
605 Finally, if none of the previous methods provided us with a value (or if none of them
606 represented a language that is supported by
607 L<Locale::Country::Multilingual|Locale::Country::Multilingual>) the last fallback will
608 be the english language ("en").
609  
610 =back
611  
612 Especially if you're using the C<I18N> plugin it's ususally your best option to not
613 explicitly set a language.
614  
615 =head1 CODESETS
616  
617 Any of the three codesets used by L<Locale::Country::Multilingual|Locale::Country::Multilingual>
618 (i.e. C<ALPHA_2>, C<ALPHA_3> and C<NUMERIC>) are accepted - but you have to make sure that you
619 consistenly use the same codeset across function calls and config values.
620  
621 =head1 METHODS/HELPERS
622  
623 =head2 country_select_field ( [ $hashref ] )
624  
625 Helper method that creates the html fragment with the select field.
626  
627 Optionally takes a hash reference with configuration values.
628 See section "CONFIGURATION" above for values which are currently allowed / recognized.
629 These values override the current configuration but are used only for the current method
630 call.
631  
632 =head2 code2country ( $code [, $language ] )
633  
634 Returns the name for the given country code.
635 The code can be from any of the three supported codesets.
636  
637 my $code = 'DE'; # ALPHA_2 code
638 my $name = $app->code2country($code); # returns "Germany" unless a
639 # different language was set
640  
641 From which language the name is taken is determined by following the fallback
642 hierarchy described above.
643  
644 Optionally you may specifiy the language to use as second param.
645  
646 my $lang = 'fr';
647 my $name = $app->code2country( $code, $lang ); # returns "Allemange"
648  
649 The value of the language parameter is used only for the current method call.
650  
651 =head2 country2code ( $name [, $language [, $codeset ] ] )
652  
653 Returns the code for the given country name:
654  
655 my $code = $self->country2code( 'Allemange', 'fr' ); # returns "DE"
656  
657 my $code = $self->country2code( 'Deutschland', 'de', 'NUMERIC' ); # returns "276"
658  
659 Make sure that the name is given in the currently configured default language
660 or specifiy the language as second param!
661  
662 If you want to get a code from a different codeset than the default ALPHA_2 you
663 can specifiy the codeset as third param.
664 If you want to specifiy a codeset but no language you need to pass an undefined
665 value as second param.
666  
667 The values given for the language and codeset parameters are used only for the
668 current method call.
669  
670 =head2 csf_country_list ( [ $hashref ] )
671  
672 Returns a hash ref indexed by the codes with the country names as values.
673  
674 Optionally takes a hash reference with configuration values.
675 See section "CONFIGURATION" above for values which are currently allowed / recognized.
676 These values override the current configuration but are used only for the current method
677 call.
678  
679 Please remember that hashes are unsorted. Therefore, the configuration entries for
680 preferred countries are also ignored.
681  
682 =head2 csf_conf ( [ $hashref ] )
683  
684 Returns a hash ref with the current configuration if no param is given.
685 Otherwise resets or updates configuration values, depending on the contents of the hash ref.
686 See section "CONFIGURATION" above for the hash keys which are currently allowed / recognized.
687  
688 $c->csf_conf( {
689 language => 'en',
690 select => 'DE',
691 prefer => [ 'DE', 'AT', 'CH', ],
692 attr => { class => 'shinycss', id => 'myId', }
693 })
694  
695 The actions taken depend on the content of the hash:
696  
697 =over 4
698  
699 =item *
700 If there's no key for a configuration entry the currently configured value is retained.
701  
702 =item *
703 If there is a key for a configuration entry but the value is undef the respective
704 configuration entry is removed (or reset to it's default value).
705  
706 =item *
707 If there is a key for a configuration entry and it carries a non-undef value, then that
708 value replaces the configured value.
709  
710 There is no way to "modify" a configuration entry (e.g. append additional html attributes)!
711  
712 =item *
713 If the hash is empty (i.e. if you call C<$c-E<gt>csf_conf({})>), the whole configuration is
714 reset.
715  
716 =back
717  
718 =head1 AUTHORS
719  
720 =over 4
721  
722 =item *
723  
724 Renee Baecker <module@renee-baecker.de>
725  
726 =item *
727  
728 Heiko Jansen <jansen@hbz-nrw.de>
729  
730 =item *
731  
732 Skye Shaw <sshaw AT lucas.cis.temple.edu>
733  
734 =back
735  
736 =head1 COPYRIGHT AND LICENSE
737  
738 This software is Copyright (c) 2012 by Hochschulbibliothekszentrum NRW (hbz).
739  
740 This is free software, licensed under:
741  
742 The GNU General Public License, Version 3, June 2007
743  
744 =cut
745  
746  
747 __END__
↑ Summary