Schema/Populate/Zone.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 51 / 51 CC 5 SCAR 11.0
CC
5
Cov
100%
records
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« Test/Role/Database.pm ↑ Summary Test/Role/Fixtures.pm »
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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Interchange6::Schema::Populate::CountryLocale;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Interchange6::Schema::Populate::StateLocale;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 ATTRIBUTES
21  
22 =head2 states_id_initial_value
23  
24 The initial value of the states_id sequence passed to L<Interchange6::Schema::Populate::StateLocale/new>. Defaults to 1.
25  
26 =cut
27  
28 4 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 3 ▶ my $self = shift;
Subroutine records: called CC=5 SCAR=16.1
Pod documented
43 3 my ( @zones, %states_by_country );
44  
45 3 my $countries = Interchange6::Schema::Populate::CountryLocale->new->records;
46  
47 3 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 3 foreach my $state (@$states) {
53 $states_by_country{ $state->{country_iso_code} }
54 192 { $state->{state_iso_code} } = $state;
55 }
56  
57 # one zone per country
58  
59 3 foreach my $country (@$countries) {
60  
61 750 my $country_name = $country->{'name'};
62 750 my $country_code = $country->{'country_iso_code'};
63  
64 750 push @zones,
65 {
66 zone => $country_name,
67 zone_countries => [ { country_iso_code => $country_code } ],
68 };
69  
70 # one zone per state
71  
72 750 ▶ if ( exists $states_by_country{$country_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $states_by_country{$country_code}) 6 744 750
73  
74 6 foreach
75 6 my $state_code ( keys %{ $states_by_country{$country_code} } )
76 {
77  
78 192 my $state = $states_by_country{$country_code}{$state_code};
79  
80 192 my $zone = $country_name . " - " . $state->{'name'};
81  
82 push @zones,
83 {
84 zone => $zone,
85 zone_countries => [ { country_iso_code => $country_code } ],
86 192 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 push @zones,
94 {
95 zone => 'US lower 48',
96 zone_countries => [ { country_iso_code => 'US' } ],
97 zone_states => [
98 147 map { { 'states_id' => $states_by_country{US}{$_}->{'states_id'} } }
99 3 grep { !/(AK|HI)/ } keys %{ $states_by_country{US} }
100 ],
101 };
102  
103 # EU member states
104 push @zones, {
105 zone => 'EU member states',
106 zone_countries => [
107 3 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 push @zones, {
115 zone => 'EU VAT countries',
116 zone_countries => [
117 3 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 3 return \@zones;
124 }
125  
126 4 1;
« Test/Role/Database.pm ↑ Summary Test/Role/Fixtures.pm »