File Coverage

blib/lib/Interchange6/Schema/Populate/StateLocale.pm
Criterion Covered Total %
statement 15 33 45.4
branch 0 8 0.0
condition 0 3 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 21 51 41.1


line stmt bran cond sub pod time code
1             package Interchange6::Schema::Populate::StateLocale;
2              
3             =head1 NAME
4              
5             Interchange6::Schema::Populate::StateLocale
6              
7             =head1 DESCRIPTION
8              
9             This module provides population capabilities for the State schema
10              
11             =cut
12              
13 3     3   12 use strict;
  3         4  
  3         100  
14 3     3   13 use warnings;
  3         19  
  3         68  
15              
16 3     3   15 use Moo;
  3         3  
  3         16  
17 3     3   787 use Interchange6::Schema::Populate::CountryLocale;
  3         6  
  3         87  
18 3     3   14 use Locale::SubCountry;
  3         5  
  3         1103  
19              
20             =head1 ATTRIBUTES
21              
22             =head2 generate_states_id
23              
24             Boolean specifiying whether the L method should add states_id. Defaults to 0.
25              
26             =cut
27              
28             has generate_states_id => (
29             is => 'ro',
30             default => 0,
31             );
32              
33             =head2 states_id_initial_value
34              
35             The initial value of the states_id sequence (only used if generate_states_id is 1). Default is 1.
36              
37             =cut
38              
39             has states_id_initial_value => (
40             is => 'ro',
41             default => 1,
42             );
43              
44             =head1 METHODS
45              
46             =head2 records
47              
48             Returns array reference containing one hash reference per state,
49             ready to use with populate schema method.
50              
51             =cut
52              
53             sub records {
54 0     0 1   my $self = shift;
55 0           my $states_id = $self->states_id_initial_value;
56              
57 0           my @states;
58 0           my $countries = Interchange6::Schema::Populate::CountryLocale->new->records;
59              
60 0           for my $country_object (@$countries) {
61 0 0         if ( $country_object->{'show_states'} == 1 ) {
62 0           my $country_code = $country_object->{'country_iso_code'};
63 0           my $country = Locale::SubCountry->new( $country_object->{'country_iso_code'} );
64              
65 0 0         next unless $country->has_sub_countries;
66              
67 0           my %country_states_keyed_by_code = $country->code_full_name_hash;
68              
69 0           foreach my $state_code ( sort keys %country_states_keyed_by_code ) {
70              
71             # some US 'states' are not actually states of the US
72             next
73 0 0 0       if ( $country_code eq 'US'
74             && $state_code =~ /(AS|GU|MP|PR|UM|VI)/ );
75              
76 0           my $state_name = $country_states_keyed_by_code{$state_code};
77              
78             # remove (Junk) from some records
79 0           $state_name =~ s/\s*\([^)]*\)//g;
80 0 0         if ( $self->generate_states_id == 1 ) {
81 0           push @states,
82             {
83             'states_id' => $states_id++,
84             'name' => $state_name,
85             'state_iso_code' => $state_code,
86             'country_iso_code' => $country_code
87             };
88             }
89             else {
90 0           push @states,
91             {
92             'name' => $state_name,
93             'state_iso_code' => $state_code,
94             'country_iso_code' => $country_code
95             };
96             }
97             }
98             }
99             }
100              
101 0           return \@states;
102             }
103              
104             1;