File Coverage

blib/lib/Data/TableReader/Field.pm
Criterion Covered Total %
statement 18 18 100.0
branch 11 12 91.6
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 33 34 97.0


line stmt bran cond sub pod time code
1             package Data::TableReader::Field;
2             $Data::TableReader::Field::VERSION = '0.010';
3 5     5   1331 use Moo 2;
  5         11596  
  5         92  
4              
5             # ABSTRACT: Field specification for Data::TableReader
6              
7              
8             has name => ( is => 'ro', required => 1 );
9             has header => ( is => 'ro' );
10             has required => ( is => 'ro', default => sub { 1 } );
11             has trim => ( is => 'ro', default => sub { 1 } );
12             has blank => ( is => 'ro' ); # default is undef
13             has type => ( is => 'ro', isa => sub { ref $_[0] eq 'CODE' or $_[0]->can('validate') } );
14             has array => ( is => 'ro' );
15             has follows => ( is => 'ro' );
16 86 100   86 1 153 sub follows_list { my $f= shift->follows; ref $f? @$f : defined $f? ( $f ) : () }
  86 50       321  
17              
18              
19             has header_regex => ( is => 'lazy' );
20              
21             sub _build_header_regex {
22 40     40   3234 my $self= shift;
23 40         105 my $h= $self->header;
24 40 100       107 unless (defined $h) {
25 29         68 $h= $self->name;
26 29         75 $h =~ s/([[:lower:]])([[:upper:]])/$1 $2/g; # split words on camelCase
27 29         64 $h =~ s/([[:alpha:]])([[:digit:]])/$1 $2/g; # or digit
28 29         44 $h =~ s/([[:digit:]])([[:alpha:]])/$1 $2/g;
29 29         61 $h =~ s/_/ /g; # then split on underscore
30             }
31 40 100       135 return $h if ref($h) eq 'Regexp';
32 36 100       182 my $pattern= join "[\\W_]*", map { $_ eq "\n"? '\n' : "\Q$_\E" } grep { defined && length }
  45 100       189  
  56         252  
33             split /(\n)|\s+|(\W)/, $h; # capture newline or non-word, except for other whitespace
34 36         3859 return qr/^[\W_]*$pattern[\W_]*$/im;
35             }
36              
37             1;
38              
39             __END__