File Coverage

blib/lib/Data/Transpose/Iterator/Errors.pm
Criterion Covered Total %
statement 22 24 91.6
branch 2 4 50.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 31 35 88.5


line stmt bran cond sub pod time code
1             package Data::Transpose::Iterator::Errors;
2              
3 11     11   39 use strict;
  11         10  
  11         249  
4 11     11   31 use warnings;
  11         11  
  11         193  
5              
6 11     11   28 use Moo;
  11         12  
  11         35  
7              
8             extends 'Data::Transpose::Iterator::Base';
9              
10             =head1 NAME
11              
12             Data::Transpose::Iterator::Errors - Iterator for validation errors.
13              
14             The errors iterator provides C method to retrieve
15             a structure suitable to show error message beside form fields.
16              
17             =head1 METHODS
18              
19             =head2 append
20              
21             Appends record to iterator.
22              
23             =cut
24              
25             sub append {
26 52     52 1 53 my ($self, $record) = @_;
27              
28 52         47 $self->records([@{$self->records}, $record]);
  52         720  
29             }
30              
31             =head2 errors_hash
32              
33             Returns records of the iterator as hash.
34             The value from "field" key is used as hash key and
35             the value from "errors" key is used as hash value.
36              
37             =cut
38              
39             sub errors_hash {
40 1     1 1 2 my ( $self ) = @_;
41 1         2 my ( %hash );
42              
43 1         2 for my $record ( @{$self->records} ) {
  1         14  
44 2 50       8 unless (exists $record->{field}) {
45 0         0 die "Missing entry for field in record.";
46             }
47 2 50       3 unless (exists $record->{errors}) {
48 0         0 die "Missing entry for errors in record.";
49             }
50              
51 3         10 $hash{$record->{field}} = [map {{name => $_->[0],
52             value => $_->[1]}}
53 2         3 @{$record->{errors}}];
  2         3  
54             }
55              
56 1         12 return \%hash;
57             }
58              
59             =head1 AUTHOR
60              
61             Stefan Hornburg (Racke),
62              
63             =head1 LICENSE AND COPYRIGHT
64              
65             Copyright 2014-2016 Stefan Hornburg (Racke) .
66              
67             This program is free software; you can redistribute it and/or modify it
68             under the terms of either: the GNU General Public License as published
69             by the Free Software Foundation; or the Artistic License.
70              
71             See http://dev.perl.org/licenses/ for more information.
72              
73             =cut
74              
75             1;
76