File Coverage

blib/lib/Data/Validate/CSV/Row.pm
Criterion Covered Total %
statement 44 62 70.9
branch 2 6 33.3
condition 2 3 66.6
subroutine 13 17 76.4
pod 0 3 0.0
total 61 91 67.0


line stmt bran cond sub pod time code
1 2     2   26 use v5.12;
  2         8  
2 2     2   32 use strict;
  2         7  
  2         72  
3 2     2   15 use warnings;
  2         5  
  2         118  
4              
5             package Data::Validate::CSV::Row;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.003';
9              
10 2     2   12 use Moo;
  2         3  
  2         24  
11 2     2   659 use Data::Validate::CSV::Types -types;
  2         5  
  2         18  
12 2     2   10589 use Types::Common::Numeric qw( PositiveOrZeroInt );
  2         5  
  2         17  
13 2     2   866 use List::Util qw(max);
  2         5  
  2         204  
14 2     2   16 use namespace::autoclean;
  2         4  
  2         14  
15              
16 2     2   169 use overload '@{}' => sub { shift->values }, fallback => 1;
  2     0   4  
  2         15  
  0         0  
17              
18             has columns => (
19             is => 'ro',
20             isa => ArrayRef[Column],
21             coerce => !!1,
22             );
23              
24             has raw_values => (
25             is => 'ro',
26             isa => ArrayRef[Str],
27             required => !!1,
28             );
29              
30             has values => (
31             is => 'lazy',
32             isa => ArrayRef[ Str | ArrayRef[Str] ],
33             );
34              
35             has _reported_errors => (
36             is => 'lazy',
37 4     4   57 builder => sub { [] },
38             );
39              
40             sub _build_values {
41 4     4   7178 my $self = shift;
42 4         11 [ map $_->value, @{ $self->cells } ];
  4         70  
43             }
44              
45             has cells => (
46             is => 'lazy',
47             isa => ArrayRef[Cell],
48             );
49              
50             has row_number => (
51             is => 'ro',
52             isa => PositiveOrZeroInt,
53             );
54              
55             has primary_key_columns => (
56             is => 'ro',
57             isa => ArrayRef->of(Str)->plus_coercions(Str, '[$_]'),
58             coerce => !!1,
59             predicate => !!1,
60             );
61              
62             has key_string => (
63             is => 'lazy',
64             isa => Str,
65             );
66              
67             has single_value_cell_class => (
68             is => 'ro',
69             isa => ClassName,
70             default => SingleValueCell->class,
71             );
72              
73             has multi_value_cell_class => (
74             is => 'ro',
75             isa => ClassName,
76             default => MultiValueCell->class,
77             );
78              
79             has column_class => (
80             is => 'ro',
81             isa => ClassName,
82             default => Column->class,
83             );
84              
85             sub _build_cells {
86 4     4   48 my $self = shift;
87 4         15 my $raws = $self->raw_values;
88 4         16 my $cols = $self->columns;
89 4         10 my @cells;
90 4         17 for my $i (0 .. $#$raws) {
91 13   66     4909 $cols->[$i] ||= $self->column_class->new;
92 13 100       159 my $class = $cols->[$i]->has_separator
93             ? $self->multi_value_cell_class
94             : $self->single_value_cell_class;
95 13         264 push @cells, $class->new(
96             raw_value => $raws->[$i],
97             row_number => $self->row_number,
98             col_number => $i + 1,
99             row => $self,
100             col => $cols->[$i],
101             );
102             }
103 4         4942 \@cells;
104             }
105              
106             sub _build_key_string {
107 0     0   0 my $self = shift;
108 0 0       0 return '' unless $self->has_primary_key_columns;
109 0         0 my %hash;
110             $hash{$_->col->name} = $_
111 0         0 for grep $_->col->has_name, @{ $self->cells };
  0         0  
112 0         0 my @cells = map $hash{$_}, @{$self->primary_key_columns};
  0         0  
113 0         0 join ',', map $_->_chunk_for_key_string, @cells;
114             }
115              
116             sub errors {
117 6     6 0 4945 my $self = shift;
118             [
119 6         149 @{ $self->_reported_errors },
120 6         12 map @{$_->errors}, @{$self->cells},
  19         161  
  6         113  
121             ];
122             }
123              
124             sub report_error {
125 0     0 0   my $self = shift;
126 0           push @{$self->_reported_errors}, @_;
  0            
127             }
128              
129             sub get {
130 0     0 0   my $self = shift;
131 0           my ($name) = @_;
132 0 0         my ($cell) = grep { $_->col->has_name and $_->col->name eq $name } @{$self->cells};
  0            
  0            
133 0           $cell;
134             }
135              
136             1;