File Coverage

blib/lib/Data/TableReader/Field.pm
Criterion Covered Total %
statement 29 30 96.6
branch 18 20 90.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 55 58 94.8


line stmt bran cond sub pod time code
1             package Data::TableReader::Field;
2 5     5   1227 use Moo 2;
  5         10978  
  5         33  
3 5     5   2382 use Carp ();
  5         9  
  5         3105  
4              
5             # ABSTRACT: Field specification for Data::TableReader
6             our $VERSION = '0.011'; # VERSION
7              
8              
9             has name => ( is => 'ro', required => 1 );
10             has header => ( is => 'ro' );
11             has required => ( is => 'ro', default => sub { 1 } );
12             has trim => ( is => 'ro', default => sub { 1 } );
13             has blank => ( is => 'ro' ); # default is undef
14             has type => ( is => 'ro', isa => sub { ref $_[0] eq 'CODE' or $_[0]->can('validate') } );
15             has array => ( is => 'ro' );
16             has follows => ( is => 'ro' );
17 94 100   94 1 127 sub follows_list { my $f= shift->follows; ref $f? @$f : defined $f? ( $f ) : () }
  94 50       288  
18              
19              
20             has header_regex => ( is => 'lazy' );
21              
22             sub _build_header_regex {
23 44     44   3177 my $self= shift;
24 44         106 my $h= $self->header;
25 44 100       105 unless (defined $h) {
26 33         76 $h= $self->name;
27 33         78 $h =~ s/([[:lower:]])([[:upper:]])/$1 $2/g; # split words on camelCase
28 33         76 $h =~ s/([[:alpha:]])([[:digit:]])/$1 $2/g; # or digit
29 33         45 $h =~ s/([[:digit:]])([[:alpha:]])/$1 $2/g;
30 33         58 $h =~ s/_/ /g; # then split on underscore
31             }
32 44 100       119 return $h if ref($h) eq 'Regexp';
33 40 100       169 my $pattern= join "[\\W_]*", map { $_ eq "\n"? '\n' : "\Q$_\E" } grep { defined && length }
  49 100       162  
  60         205  
34             split /(\n)|\s+|(\W)/, $h; # capture newline or non-word, except for other whitespace
35 40         3712 return qr/^[\W_]*$pattern[\W_]*$/im;
36             }
37              
38             has trim_coderef => ( is => 'lazy' );
39              
40             sub _default_trim_coderef {
41 91     91   2814 $_ =~ s/\s+$//;
42 91         217 $_ =~ s/^\s+//;
43             }
44              
45             sub _build_trim_coderef {
46 39     39   1728 my $t= shift->trim;
47 39 100       75 return undef unless $t;
48 38 100       141 return \&_default_trim_coderef if !ref $t;
49 4 100       18 return $t if ref $t eq 'CODE';
50 2 50   8   15 return sub { s/$t//g; } if ref $t eq 'Regexp';
  8         3984  
51 0           Carp::croak("Can't convert ".ref($t)." to a coderef");
52             }
53              
54             1;
55              
56             __END__