File Coverage

blib/lib/Interchange6/Schema/Populate/Zone.pm
Criterion Covered Total %
statement 15 40 37.5
branch 0 2 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 21 49 42.8


line stmt bran cond sub pod time code
1             package Interchange6::Schema::Populate::Zone;
2              
3             =head1 NAME
4              
5             Interchange6::Schema::Populate::Zone
6              
7             =head1 DESCRIPTION
8              
9             This module provides population capabilities for the Zone schema
10              
11             =cut
12              
13 3     3   13 use strict;
  3         5  
  3         97  
14 3     3   10 use warnings;
  3         5  
  3         93  
15              
16 3     3   11 use Moo;
  3         3  
  3         12  
17 3     3   704 use Interchange6::Schema::Populate::CountryLocale;
  3         5  
  3         47  
18 3     3   11 use Interchange6::Schema::Populate::StateLocale;
  3         4  
  3         1052  
19              
20             =head1 ATTRIBUTES
21              
22             =head2 states_id_initial_value
23              
24             The initial value of the states_id sequence passed to L. Defaults to 1.
25              
26             =cut
27              
28             has states_id_initial_value => (
29             is => 'ro',
30             default => 1,
31             );
32              
33             =head1 METHODS
34              
35             =head2 records
36              
37             Returns array reference containing one hash reference per zone ready to use with populate schema method.
38              
39             =cut
40              
41             sub records {
42 0     0 1   my $self = shift;
43 0           my ( @zones, %states_by_country );
44              
45 0           my $countries = Interchange6::Schema::Populate::CountryLocale->new->records;
46              
47 0           my $states = Interchange6::Schema::Populate::StateLocale->new(
48             { generate_states_id => 1,
49             states_id_initial_value => $self->states_id_initial_value,
50             } )->records;
51              
52 0           foreach my $state (@$states) {
53 0           $states_by_country{ $state->{country_iso_code} }
54             { $state->{state_iso_code} } = $state;
55             }
56              
57             # one zone per country
58              
59 0           foreach my $country (@$countries) {
60              
61 0           my $country_name = $country->{'name'};
62 0           my $country_code = $country->{'country_iso_code'};
63              
64 0           push @zones,
65             {
66             zone => $country_name,
67             zone_countries => [ { country_iso_code => $country_code } ],
68             };
69              
70             # one zone per state
71              
72 0 0         if ( exists $states_by_country{$country_code} ) {
73              
74 0           foreach
75 0           my $state_code ( keys %{ $states_by_country{$country_code} } )
76             {
77              
78 0           my $state = $states_by_country{$country_code}{$state_code};
79              
80 0           my $zone = $country_name . " - " . $state->{'name'};
81              
82 0           push @zones,
83             {
84             zone => $zone,
85             zone_countries => [ { country_iso_code => $country_code } ],
86             zone_states => [ { states_id => $state->{'states_id'} } ],
87             };
88             }
89             }
90             }
91              
92             # US lower 48 includes all 51 from US except for Alaska and Hawaii
93 0           push @zones,
94             {
95             zone => 'US lower 48',
96             zone_countries => [ { country_iso_code => 'US' } ],
97             zone_states => [
98 0           map { { 'states_id' => $states_by_country{US}{$_}->{'states_id'} } }
99 0           grep { !/(AK|HI)/ } keys %{ $states_by_country{US} }
  0            
100             ],
101             };
102              
103             # EU member states
104 0           push @zones, {
105             zone => 'EU member states',
106             zone_countries => [
107 0           map { { 'country_iso_code' => $_ } }
108             qw ( BE BG CZ DK DE EE GR ES FR HR IE IT CY LV LT LU HU MT
109             NL AT PL PT RO SI SK FI SE GB )
110             ],
111             };
112              
113             # EU VAT countries = EU + Isle of Man
114 0           push @zones, {
115             zone => 'EU VAT countries',
116             zone_countries => [
117 0           map { { 'country_iso_code' => $_ } }
118             qw ( BE BG CZ DK DE EE GR ES FR HR IE IT CY LV LT LU HU MT
119             NL AT PL PT RO SI SK FI SE GB IM )
120             ],
121             };
122              
123 0           return \@zones;
124             }
125              
126             1;