File Coverage

blib/lib/String/Normal/Type/State.pm
Criterion Covered Total %
statement 17 19 89.4
branch 4 6 66.6
condition 1 2 50.0
subroutine 5 5 100.0
pod 2 2 100.0
total 29 34 85.2


line stmt bran cond sub pod time code
1             package String::Normal::Type::State;
2 9     9   29 use strict;
  9         10  
  9         187  
3 9     9   25 use warnings;
  9         9  
  9         150  
4              
5 9     9   26 use String::Normal::Config::States;
  9         8  
  9         1385  
6              
7             our $codes;
8              
9             sub transform {
10 2     2 1 3 my ($self,$value) = @_;
11              
12 2 100       10 if (length($value) > 2) {
    50          
13             # convert long name to short name (with potential invalidation)
14 1   50     3 $value = $codes->{by_long}{$value} || '';
15             }
16             elsif (! $codes->{by_short}{$value}) {
17             # invalidate short name
18 0         0 $value = '';
19             }
20              
21             # now validate against country code
22             # TODO: contemplate moving country awareness to a higher level
23             # if more fields require such validation (hint ... zipcodes)
24             # if ($values[$schema{country}] eq 'US') {
25             # $value = '' unless $us_codes->{$value};
26             # }
27             # elsif ($values[$schema{country}] eq 'CA') {
28             # $value = '' unless $ca_codes->{$value};
29             # }
30              
31 2 50       4 if ($value) {
32 2         8 return $value;
33             }
34             else {
35 0         0 die "invalid state";
36             }
37              
38             }
39              
40             sub new {
41 1     1 1 2 my $self = shift;
42 1         4 $codes = String::Normal::Config::States::_data( @_ );
43 1         4 return bless {@_}, $self;
44             }
45              
46             1;
47              
48             __END__