File Coverage

blib/lib/Data/Validate/CSV/Row.pm
Criterion Covered Total %
statement 26 63 41.2
branch 0 6 0.0
condition 0 3 0.0
subroutine 9 17 52.9
pod 0 3 0.0
total 35 92 38.0


line stmt bran cond sub pod time code
1 1     1   14 use v5.12;
  1         4  
2 1     1   6 use strict;
  1         2  
  1         24  
3 1     1   6 use warnings;
  1         2  
  1         65  
4              
5             package Data::Validate::CSV::Row;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.002';
9              
10 1     1   8 use Moo;
  1         4  
  1         6  
11 1     1   349 use Data::Validate::CSV::Types -types;
  1         3  
  1         9  
12 1     1   5291 use Types::Common::Numeric qw( PositiveOrZeroInt );
  1         2  
  1         11  
13 1     1   445 use List::Util qw(max);
  1         3  
  1         80  
14 1     1   6 use namespace::autoclean;
  1         2  
  1         7  
15              
16 1     1   92 use overload '@{}' => sub { shift->values }, fallback => 1;
  1     0   2  
  1         9  
  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 0     0     builder => sub { [] },
38             );
39              
40             sub _build_values {
41 0     0     my $self = shift;
42 0           [ map $_->value, @{ $self->cells } ];
  0            
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 0     0     my $self = shift;
87 0           my $raws = $self->raw_values;
88 0           my $cols = $self->columns;
89 0           my $max = max($#$raws, $#$cols);
90 0           my @cells;
91 0           for my $i (0 .. $max) {
92 0   0       $cols->[$i] ||= $self->column_class->new;
93 0 0         my $class = $cols->[$i]->has_separator
94             ? $self->multi_value_cell_class
95             : $self->single_value_cell_class;
96 0           push @cells, $class->new(
97             raw_value => $raws->[$i],
98             row_number => $self->row_number,
99             col_number => $i + 1,
100             row => $self,
101             col => $cols->[$i],
102             );
103             }
104 0           \@cells;
105             }
106              
107             sub _build_key_string {
108 0     0     my $self = shift;
109 0 0         return '' unless $self->has_primary_key_columns;
110 0           my %hash;
111             $hash{$_->col->name} = $_
112 0           for grep $_->col->has_name, @{ $self->cells };
  0            
113 0           my @cells = map $hash{$_}, @{$self->primary_key_columns};
  0            
114 0           join ',', map $_->_chunk_for_key_string, @cells;
115             }
116              
117             sub errors {
118 0     0 0   my $self = shift;
119             [
120 0           @{ $self->_reported_errors },
121 0           map @{$_->errors}, @{$self->cells},
  0            
  0            
122             ];
123             }
124              
125             sub report_error {
126 0     0 0   my $self = shift;
127 0           push @{$self->_reported_errors}, @_;
  0            
128             }
129              
130             sub get {
131 0     0 0   my $self = shift;
132 0           my ($name) = @_;
133 0 0         my ($cell) = grep { $_->col->has_name and $_->col->name eq $name } @{$self->cells};
  0            
  0            
134 0           $cell;
135             }
136              
137             1;